基于JQuery实现的文本框自动填充功能

时间:2022-02-01 02:11:12

1. 实现的方法

 /*
* js实现的文本框的自动完成功能
*/
function doAutoComplete(textid,dataid,url){
$("#" + textid).autocomplete({
serviceUrl: url,
paramName: "name",
delimiter: ",",
transformResult: function(response) {
return {
suggestions: $.map($.parseJSON(response), function(item) {
return { value: item.tagName, data: item.id };
})
};
},
onSelect: function (suggestion) {
$("#" + dataid).val(suggestion.data);
}
});
};

doAutoComplete

2. 调用方法

<script src="<c:url value="/scripts/jquery/jquery.1.10.2.min.js" />"></script>
<script src="<c:url value="/scripts/jquery/jquery.autocomplete.min.js" />"></script>
<script src="<c:url value="/scripts/my.js" />"></script>

<script type="text/javascript">
  doAutoComplete("userName","userId","${pageContext.request.contextPath}/userform/getUserList");
</script>

附:jquery.autocomplete.js的下载地址