自动完成未在文本框中显示建议值。但后端查询执行并得到结果,它没有显示在文本框中。请告诉我如何在文本字段中显示建议框。
<!DOCTYPE>
<html>
<head>
<title>Auto Complete in JSP Java</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script>
$(function() {
$("#names").autocomplete({
source: function(request, response) {
$.ajax({
url: "searchName.jsp",
type: "POST",
dataType: "text",
data: { name: request.term},
success: function( data ) {
//alert(data);
response( $.map( data, function( item ) {
return {
label: item.name,
value: item.value,
}
}));
},
error: function (error) {
alert('error: ' + error);
}
});
},
minLength: 3
});
});
</script>
</head>
<body>
<input type="text" name="name" id="names" />
</body>
</html>
搜索名称.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%
String query = (String)request.getParameter("name");
try{
String s[]=null;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con DriverManager.getConnection("XXXX");
Statement st=con.createStatement();
ResultSet rs = st.executeQuery("select name from Table1 where name like '"+query+"%'");
List li = new ArrayList();
while(rs.next())
{
li.add(rs.getString(1));
}
String[] str = new String[li.size()];
Iterator it = li.iterator();
int i = 0;
while(it.hasNext())
{
String p = (String)it.next();
str[i] = p;
i++;
}
int cnt=1;
for(int j=0;j<str.length;j++)
{
if(str[j].toUpperCase().startsWith(query.toUpperCase()))
{
out.print(str[j]+"\n");
if(cnt>=5)// 5=How many results have to show while we are typing(auto suggestions)
break;
cnt++;
}
}
rs.close();
st.close();
con.close();
}
catch(Exception e){
e.printStackTrace();
}
%>
请给我提意见...