欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jquery自动补全

程序员文章站 2022-06-01 12:57:11
...
可运行的jquery自动补全 jsp部分

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="${ctx}/css/default.css" rel="stylesheet" type="text/css" />
<link href="${ctx}/css/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
<script src="${ctx}/js/jquery.autocomplete.js" type="text/javascript"></script>



<script type="text/javascript">
function findValue(li) {
if( li == null ) return alert("No match!");

// if coming from an AJAX call, let's use the CityId as the value
if( !!li.extra ) var sValue = li.extra[0];

// otherwise, let's just display the value in the text box
else var sValue = li.selectValue;

//alert("The value you selected was: " + sValue);
}

function selectItem(li) {
findValue(li);
}

function formatItem(row) {
return row[0];
}


function lookupLocal(){
var oSuggest = $("#autoname")[0].autocompleter;

oSuggest.findValue();

return false;
}

$(function() {

$("#autoname").autocomplete(
"${ctx}/goods/ajaxAutoName.action",
{
delay:10,
minChars:1,
matchSubset:1,
matchContains:1,
cacheLength:10,
onItemSelect:selectItem,
onFindValue:findValue,
formatItem:formatItem,
autoFill:true
}
);
});

</script>
</head>


<td><input type="text" id="autoname" class="ac_input" name="memberName"/></td>


java 部分

public String ajaxAutoName(){
String keyword=request.getParameter("q");
List<String> memberList=memberService.getMemberByLike(keyword);
try {
response.setContentType("text/plain; charset=GBK");
PrintWriter out = response.getWriter();
for (String str : memberList){
out.println(str);
}

out.flush();
out.close();
return null;
} catch (Exception e) {
e.printStackTrace();

}
return null;
}