1. 實現的方法
1 /* 2 * js實現的文字框的自動完成功能 3 */ 4 function doAutoComplete(textid,dataid,url){ 5 $("#" + textid).autocomplete({ 6 serviceUrl: url, 7 paramName: "name", 8 delimiter: ",", 9 transformResult: function(response) { 10 return { 11 suggestions: $.map($.parseJSON(response), function(item) { 12 return { value: item.tagName, data: item.id }; 13 }) 14 }; 15 }, 16 onSelect: function (suggestion) { 17 $("#" + dataid).val(suggestion.data); 18 } 19 }); 20 };
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的下載地址