<!-- 需要的js css -->
<link href="<%=basePath%>js/autocomplete/jquery-ui-1.10.3.custom.css" rel="stylesheet">
<script src="<%=basePath%>js/jquery-1.7.2.min.js"></script>
<script src="<%=basePath%>js/autocomplete/jquery-ui-1.10.3.custom.js"></script>
<script>
$(function(){
$( "#autocomplete" ).autocomplete({
minLength: 1,//最少输入1个字符才执行
source: 'autocomplete.action'
});
})
</scrip
定义一个文本框
<input id="autocomplete" title="type "a"">
java 代码
try {
//brandName="Jörn Zaefferer, Scott González, John Resig,桑陈,何心远,";
String[] temp = "align,both,card,dream,error,fail,gson,hello,invaid,job,kill".split(",");
String name = "[";
for(int i=0;i<temp.length;i++){
if(temp[i].indexOf(temp[i])!=-1){
name += "\""+temp[i]+"\",";
}
}
if(name.length() > 1)
name = name.substring(0,name.length()-1);
name+="]";
System.out.println(name);
HttpServletResponse res = ServletActionContext.getResponse();
PrintWriter pw = res.getWriter();
pw.write(name);
pw.flush();
pw.close();
return null;
} catch (Exception e) {
// TODO: handle exception
}
效果图
注:因为数据是我后台写死的