jquery 自动补全

1.servlet中

public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  String word = request.getParameter("word");
  request.setAttribute("word", word);
  System.out.println("dd");
  request.getRequestDispatcher("testXml.jsp").forward(request, response);
 }

2.建一个xml文件(testXml.jsp)

<%@ page contentType="text/xml;charset=utf-8" language="java"%>
<words>
 <word>aafdfds</word>
 <word>afdgwe</word>
 <word>aewgsdf</word>
 <word>agwefsd</word>
 <word>aaafdsf</word>
 <word>afewtge</word>
 <word>bbsfa</word>
 <word>bbfsf</word>
 <word>bfsfs</word>
</words>

3.Html文件

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
 <link rel="stylesheet" type="text/css" href="./jQuery/css/test.css">
 <script type="text/javascript" src="./jQuery/js/jquery.js"></script>
 <script type="text/javascript" src="./jQuery/test/test_8.js"></script>
 <style type="text/css">
  div{
   table-layout: auto;
  }
 </style>
  </head>
   <!-- 实现仿GoogleSuggest自动补全的功能  -->
  <body>
   <input type="text" id="word"/>
   <input type="button" value="提交"/>
   <div id="auto"></div>
  </body>
</html>

4.建一个js文件(test_8.js)

var currIndex = -1;
$(function(){
 var timeInterval;
 var word = $("#word");
 //设置样式
 $("#auto").css("border","1px solid black")
    .css("position","absolute")
    .css("top",word.offset().top+word.height()+1+"px")
    .css("left",word.offset().left+"px")
    .css("width",word.width()+"px")
    .hide();
 
 //添加键盘事件
 word.keyup(function(e){
  var e = e || window.event;
 
  //如查输入的是字母或退格或删除
  if(e.keyCode >= 65 && e.keyCode <= 90 || e.keyCode == 8 || e.keyCode == 46){
    var auto = $("#auto");
    auto.html("");
    auto.hide();
    currIndex = -1;
    var value = word.val();
    
   //清除事件
   clearTimeout(timeInterval);
   
   if(value != ""){
    //延迟发送
    timeInterval = setTimeout(ajaxPost,500);
   }
   
  }else if(e.keyCode == 38 || e.keyCode == 40){
   //向上或向下 
   
   if(e.keyCode == 38){//向上
    var divs = $("#auto > div");
    if(currIndex != -1){
     divs.eq(currIndex).css("background-color","white");
    }
    currIndex--;
    if(currIndex == -1){
     currIndex = divs.length - 1;
    }
    divs.eq(currIndex).css("background-color","red");
   }
   if(e.keyCode == 40){//向下
    var divs = $("#auto > div");
    if(currIndex != -1){
     divs.eq(currIndex).css("background-color","white");
    }
    currIndex++;
    if(currIndex == divs.length){
     currIndex = 0;
    }
    divs.eq(currIndex).css("background-color","red");
   }
  }else if(e.keyCode == 13){
   //回车
   //alert("dd");
   var divs = $("#auto > div");
   var value = divs.eq(currIndex).text();
   $("#word").val(value);
   $("#auto").hide();
   currIndex = -1;
  }
 });
 
 $("input[type='button']").click(function(){
  alert("文本框中的内容是:"+word.val());
 });
});

function ajaxPost(){
 var value = $("#word").val();
 var auto = $("#auto");
 $.post("Test8Servlet",{word:value},function(data){
     //得到所有的word节点
     var words = $(data).find("word");//处理XML
     words.each(function(w){
      var wdiv = $("<div>").attr("id",w);
      wdiv.html($(this).text()).appendTo(auto);
      //添加鼠标事件
      wdiv.mouseover(function(){
       if(currIndex != -1){
        divs = $("#auto > div").eq(currIndex).css("background-color","white");
       }
       currIndex = this.id;
       $(this).css("background-color","red");
      });
      
      wdiv.mouseout(function(){
       $(this).css("background-color","white");
      });
      
      wdiv.click(function(){
       $("#word").val($(this).text());
       $("#auto").hide();
       currIndex = -1;
      });
      
     });
     //alert(words.length);
     if(words.length > 0){
      auto.show();
     }else{
      auto.hide();
      currIndex = -1;
     }
     
    },"xml");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值