JqueryAutoComplete.html


  
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  2. "http://www.w3.org/TR/html4/loose.dtd">  
  3. <html>  
  4. <head>  
  5. <title>Jquery autoComplete</title>  
  6. <meta http-equiv="Content-Type" content="text/html;gb2312">  
  7. <script type="text/javascript" src="js/jquery.js"></script>  
  8. <script type="text/javascript" src="js/jqueryautocomplete.js"></script>  
  9. </head>  
  10. <body>  
  11. <h3>Jquery autoComplete</h3>  
  12. 用Jquery实现模仿Google 搜索提示功能  
  13. <input type="text" id="word" size="20"/>  
  14. <input type="button" value="提交"/><br>  
  15. <div id="auto"></div>  
  16. </body>  
  17. </html> 

jqueryautocomplete.js


  
  1. // 显示高亮对应的数组的索引  
  2. var highlightindex = -1;  
  3. //提示延迟  
  4. var timeOutId;  
  5. $(document).ready(function() {  
  6. //取得输入框对象  
  7. var wordinput = $("#word");  
  8. //获取输入框在当前视口的相对偏移  
  9. var wordinputoffset = wordinput.offset();  
  10. //设置提示框的相关参数  
  11. $("#auto").hide().css("border""1px black solid")  
  12. .css("position""absolute")  
  13. .css("top", wordinputoffset.top + wordinput.height() + 5 + "px")  
  14. .css("left", wordinputoffset.left + "px")  
  15. .width(wordinput.width() + 2 + "px");  
  16. //键盘事件  
  17. $("#word").keyup(function(event) {  
  18. var myEvent = event || window.event;  
  19. //获取当前键值  
  20. var keyCode = myEvent.keyCode;  
  21. // 按下字母键和退格、delete键  
  22. if (keyCode >= 65 && keyCode <= 90 || keyCode == 8 || keyCode == 46) {  
  23. var wordText = $("#word").val();  
  24. var autoNode = $("#auto");  
  25. //只有输入框中有值时才向服务器发送请求  
  26. if (wordText != "") {  
  27. //取消上次未完成的操作  
  28. clearTimeout(timeOutId);  
  29. //对服务器的请求延迟200ms  
  30. timeOutId = setTimeout(function(){  
  31. $.post("AutoComplete", {word:wordText}, function(data) {  
  32. //将dom对象转化为jquery对象   
  33. var jqueryObj = $(data);  
  34. //找到所有的word节点  
  35. var wordNodes = jqueryObj.find("word");  
  36. //遍历前清空原来的内容  
  37. autoNode.empty();  
  38. wordNodes.each(function(i) {  
  39. var wordNode = $(this);  
  40. var newDivNode = $("<div>").attr("id", i);  
  41. newDivNode.html(wordNode.text()).appendTo(autoNode);  
  42. //鼠标事件  
  43. newDivNode.mouseover(function() {  
  44. if (highlightindex != -1) {  
  45. $("#auto").children("div").eq(highlightindex).css("background-color""white");  
  46. }  
  47. highlightindex = $(this).attr("id");  
  48. $(this).css("background-color""red");  
  49. });  
  50. newDivNode.mouseout(function() {  
  51. $(this).css("background-color""white");  
  52. });  
  53. //点击获取选中的值  
  54. newDivNode.click(function(){  
  55. $("#word").val( $(this).text());  
  56. highlightindex = -1;  
  57. $("#auto").hide();  
  58. });  
  59. });  
  60. if (wordNodes.length > 0) {  
  61. autoNode.show();  
  62. else {  
  63. autoNode.hide();  
  64. highlightindex = -1;  
  65. }  
  66. }, "xml");  
  67. },200);  
  68. }  
  69. else {  
  70. autoNode.hide();  
  71. highlightindex = -1;  
  72. }  
  73. }  
  74. // 按下上下键  
  75. else if (keyCode == 38 || keyCode == 40) {  
  76. //按下向上键后  
  77. if (keyCode == 38) {  
  78. //取得提示框中的各个div  
  79. var autoNodes = $("#auto").children("div");  
  80. if (highlightindex != -1) {  
  81. //如果当前有高亮节点,则把该高亮节点背景色变为非高亮背景色  
  82. autoNodes.eq(highlightindex).css("background-color""white");  
  83. highlightindex --;  
  84. else {  
  85. highlightindex = autoNodes.length - 1;  
  86. }  
  87. if (highlightindex == -1) {  
  88. highlightindex = autoNodes.length - 1;  
  89. }  
  90. autoNodes.eq(highlightindex).css("background-color""red");  
  91. }  
  92. //按下向下键后  
  93. if (keyCode == 40) {  
  94. var autoNodes = $("#auto").children("div");  
  95. if (highlightindex != -1) {  
  96. //如果当前有高亮节点,则把该高亮节点背景色变为非高亮背景色  
  97. autoNodes.eq(highlightindex).css("background-color""white");  
  98. highlightindex ++;  
  99. else {  
  100. highlightindex = 0;  
  101. }  
  102. if (highlightindex == autoNodes.length) {  
  103. highlightindex = 0;  
  104. }  
  105. autoNodes.eq(highlightindex).css("background-color""red");  
  106. }  
  107. }  
  108. // 按下回车  
  109. else if (keyCode == 13) {  
  110. if (highlightindex != -1) {  
  111. var conText = $("#auto").hide().children("div").eq(highlightindex).text();  
  112. highlightindex = -1;  
  113. $("#word").val(conText);  
  114. else {  
  115. alert("文本框中的内容【" + $("#word").val() + "】被提交了");  
  116. $("#auto").hide();  
  117. $("#word").blur();  
  118. }  
  119. }  
  120. });  
  121. $("input[type='button']").click(function() {  
  122. alert("文本框中的内容【" + $("#word").val() + "】被提交了");  
  123. });  
  124. }); 

AutoComplete.java(别忘了在web.xml里面配置哦!)


  
  1. import com.sun.deploy.net.HttpRequest;  
  2.  
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.ServletException;  
  7. import java.io.IOException;  
  8.  
  9. /**  
  10. * Created by IntelliJ IDEA.  
  11. User: Administrator  
  12. Date: 2008-9-24  
  13. Time: 21:39:17  
  14. To change this template use File | Settings | File Templates.  
  15. */  
  16. public class AutoComplete extends HttpServlet {  
  17. protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {  
  18. String word = httpServletRequest.getParameter("word");  
  19. httpServletRequest.setAttribute("word", word);  
  20. httpServletRequest.getRequestDispatcher("wordxml.jsp").forward(httpServletRequest, httpServletResponse);  
  21. // httpServletResponse.sendRedirect("wordxml.jsp");  
  22. }  
  23.  
  24. protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {  
  25. doGet(httpServletRequest, httpServletResponse);  
  26. }  

wordxml.jsp


  
  1. <%@ page contentType="text/xml;charset=UTF-8" language="java" %>  
  2. <%  
  3. String word = (String)request.getAttribute("word");  
  4. %>  
  5. <words>  
  6. <% if(("absolute").startsWith(word)){ %>  
  7. <word>absolute</word>  
  8. <%}%>  
  9. <% if(("apple").startsWith(word)){ %>  
  10. <word>apple</word>  
  11. <%}%>  
  12. <% if(("anything").startsWith(word)){ %>  
  13. <word>anything</word>  
  14. <%}%>  
  15. <% if(("anybody").startsWith(word)){ %>  
  16. <word>anybody</word>  
  17. <%}%>  
  18. <% if(("body").startsWith(word)){ %>  
  19. <word>body</word>  
  20. <%}%>  
  21. <% if(("bobo").startsWith(word)){ %>  
  22. <word>bobo</word>  
  23. <%}%>  
  24. <% if(("baby").startsWith(word)){ %>  
  25. <word>baby</word>  
  26. <%}%>  
  27. <% if(("cut").startsWith(word)){ %>  
  28. <word>cut</word>  
  29. <%}%>  
  30. <% if(("cetuegte").startsWith(word)){ %>  
  31. <word>cetuegte</word>  
  32. <%}%>  
  33. </words>