java web在不使用任何插件情况下实现文本框输入自动补全功能
2014年07月29日 11:14:12
阅读数:947
前台部分:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > < html > < head > < title > word.html </ title > < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" > < meta http-equiv = "description" content = "this is my page" > < meta http-equiv = "content-type" content = "text/html; charset=UTF-8" > < script type = "text/javascript" src = "js/jquery.js" > </ script > < script type = "text/javascript" src = "js/auto.js" > </ script > </ head > < body > suggest:< input id = "word" type = "text" /> < input type = "button" value = "提交" /> 可以尝试输入大写字母"A、B、T"将有提示 < div id = "auto" > </ div > </ body > </ html >
JS部分:
$(document).ready( function (){ var highlightindex = -1; var timeout; var wordInput = $( "#word" ); var wordInputOffset = wordInput.offset(); var autoNode = $( "#auto" ); autoNode.hide(); autoNode.css("position" , "absolute" ); autoNode.css("z-index" , "99" ); autoNode.css("cursor" , "default" ); autoNode.css("filter" , "alpha(opacity=50" ); autoNode.css("text-overflow" , "clip" ); autoNode.css("border" , "1px black solid" ); autoNode.css("top" ,wordInputOffset.top+wordInput.height()+4+ "px" ); autoNode.css("left" ,wordInputOffset.left-2+ "px" ).width(wordInput.width()+5); wordInput.blur(function () { setTimeout(function () { autoNode.hide(); highlightindex = -1; },200); }); wordInput.keyup(function (event) { var myEvent = event || window.event; var keyCode = myEvent.keyCode; if (keyCode>=48&&keyCode<=57||keyCode >= 65 && keyCode <= 90 || keyCode==8 || keyCode ==46||keyCode==13||keyCode==32) { var textInput = wordInput.val(); if (textInput != "" ) { clearTimeout(timeout); timeout = setTimeout(function (){ $.post("servlet/InputServlet" ,{text:textInput}, function (data) { var jqueryObj = $(data); var wordNodes = jqueryObj.find( "word" ); autoNode.html("" ); wordNodes.each(function (i) { var wordNode = $( this ); var NewNode = $( "<div>" ).attr( "id" ,i); NewNode.html(wordNode.text()).appendTo(autoNode); NewNode.hover( function () { if (highlightindex !=-1) { autoNode.children().eq(highlightindex).css("background-color" , "white" ); } highlightindex = $(this ).attr( "id" ); $(this ).css( "background-color" , "gray" ); }, function (){ $(this ).css( "background-color" , "white" ); } ); NewNode.click(function () { var clickNode = $( this ); wordInput.val(clickNode.text()); autoNode.hide(); highlightindex =-1; }); }); if (wordNodes.length>0) { autoNode.show(); } else { autoNode.hide(); } },"xml" ); },500); } else { autoNode.hide(); highlightindex = -1; } } else if (keyCode ==38 || keyCode ==40) { var autoNodes = autoNode.children(); if (keyCode == 38) { if (highlightindex != -1) { autoNodes.eq(highlightindex).css("background-color" , "white" ); highlightindex--; } if (highlightindex == -1){ highlightindex = autoNodes.length - 1; } autoNodes.eq(highlightindex).css("background-color" , "gray" ); wordInput.val(autoNodes.eq(highlightindex).text()); } if (keyCode == 40) { if (highlightindex!=-1) { autoNodes.eq(highlightindex).css("background-color" , "white" ); highlightindex++; } if (highlightindex==-1) { highlightindex++; } if (highlightindex == autoNodes.length) { highlightindex = 0; } autoNodes.eq(highlightindex).css("background-color" , "gray" ); wordInput.val(autoNodes.eq(highlightindex).text()); } } else if (keyCode==13) { autoNode.hide(); highlightindex = -1; } }); });
后台部分:
package servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class InputServlet extends HttpServlet { public void doGet(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8" ); String text = request.getParameter("text" ); System.out.println(text); List<String> word = new ArrayList<String>(); List<String> list = new ArrayList<String>(); list.add("Google" ); list.add("NetEase" ); list.add("Sohu" ); list.add("Sina" ); list.add("Sogou" ); list.add("Baidu" ); list.add("Tencent" ); list.add("Taobao" ); list.add("Tom" ); list.add("Yahoo" ); list.add("JavaEye" ); list.add("Csdn" ); list.add("Alipay" ); for ( int i = 0 ; i < list.size(); i++) { if (list.get(i).startsWith(text)) { word.add(list.get(i)); } } request.setAttribute("word" , word); request.getRequestDispatcher("/wordxml.jsp" ).forward(request, response); } public void doPost(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
资源下载地址:http://download.csdn.net/detail/sunny0703/7690327