寫在前面的話:官網才是靠譜的,中文部落格隻能幫助了解
官網位址:
Autocomplete | jQuery UI
http://jqueryui.com/autocomplete/#remote
依賴于:
jquery.js
jquery-ui.js
jquery-ui.css
(注意,一定要下同一版本的——
直接在官網把給出的位址改成自己要的版本,輸入到位址欄再儲存下來就好,去官網看了就會懂的)
我的代碼:
jsp:
<input type="text" id="txtKeyword" >
js:
$.get("product", function(data){
$("#txtKeyword").autocomplete({
source: data,
minLength: 0
}).focus(function(){
$(this).data("autocomplete").search($(this).val());
});
});
java:
@ResponseBody
@CrossOrigin(origins = "*")
@RequestMapping(value = "product", method = RequestMethod.GET)
public List<String> product(){
return productService.getProduct();
}
SQl語句啥的,就自己寫啦,多研究官網吧
然後記錄一個我失敗的嘗試:
關于jquery.autocomplete.js的使用,我是用ajax請求資料庫的_百度知道 http://zhidao.baidu.com/question/499927554358011644.html
補充:自定義顯示格式;focus 事件;select 事件;增加自定義的參數
jQuery UI AutoComplete 中使用自定義的資料 - 冠軍 - 部落格園
http://www.cnblogs.com/haogj/archive/2011/06/18/2084384.html