Search <input
type="text" name="name" class="inputtable"
id="name" />
<input type="image" src="images/serchbutton.gif"
border="0" style="margin-bottom:-4px">
<div id="context1" style="background-color:white; border: 1px solid red;width:128px;position: absolute;top: 133px;left:944px;display:none" > </div>
$(".inputtable").keyup(function(){
var content=$(this).val();
//如果当前搜索内容为空,无须进行查询
if(content==""){
$("#context1").css("display","none");
return ;
}
//由于浏览器的缓存机制 所以我们每次传入一个时间
var time=new Date().getTime();
$.ajax({
type:"get",
//新建一个名为findBooksAjaxServlet的servlet
url:"${pageContext.request.contextPath}/servlet/findBooksAjaxServlet",
data:{name:content,time:time},
success:function(data){
//拼接html
var res=data.split(",");
var html="";
for(var i=0;i<res.length;i++){
//每一个div还有鼠标移出、移入点击事件
html+="<div onclick='setSearch_onclick(this)' onmouseout='changeBackColor_out(this)' onmouseover='changeBackColor_over(this)'>"+res[i]+"</div>";
}
$("#context1").html(html);
//显示为块级元素
$("#context1").css("display","block");
}
});
});
var html="";
for(var i=0;i<res.length;i++){
//每一个div还有鼠标移出、移入点击事件
html+="<div onclick='setSearch_onclick(this)' onmouseout='changeBackColor_out(this)' onmouseover='changeBackColor_over(this)'>"+res[i]+"</div>";
}
//鼠标移动到内容上
function changeBackColor_over(div){
$(div).css("background-color","#CCCCCC");
}
//鼠标离开内容
function changeBackColor_out(div){
$(div).css("background-color","");
}
//将点击的内容放到搜索框
function setSearch_onclick(div){
$(".inputtable").val(div.innerText);
$("#context1").css("display","none");
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
//获取搜索框输入的内容
String name=request.getParameter("name");
name=new String(name.getBytes("iso-8859-1"), "utf-8");
//向server层调用相应的业务
BooksServer booksServer=new BooksServer();
String res=booksServer.findBooksAjax(name);
//返回结果
response.getWriter().write(res);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
//查询所有的书本名
public String findBooksAjax(String name) {
List<Object> nameList=booksDao.findBooksAjax(name);
String res="";
for (int i=0;i<nameList.size();i++) {
if(i>0){
res+=","+nameList.get(i);
}else{
res+=nameList.get(i);
}
}
return res;
}
//根据部分书名,查询类似的书名
public List<Object> findBooksAjax(String name) {
QueryRunner qr=new QueryRunner(C3P0Util.getDataSource());
try {
return qr.query("select name from book where name like ?", new ColumnListHandler(),"%"+name+"%");
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有