廢話不多說(shuō),先看效果!~
創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站建設(shè)、做網(wǎng)站與策劃設(shè)計(jì),定州網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:定州等地區(qū)。定州做網(wǎng)站價(jià)格咨詢:18982081108
需要引入的資源如下
<link rel="stylesheet" href="/css/jquery.autocomplete.css" type="text/css"> <script type="text/javascript" src="/lib/jquery.min.js"></script><!--1.8.3--> <script type="text/javascript" src="/js/jquery.autocomplete.js"></script>
前端代碼
<div class="control-group span6"> <label class="control-label">客戶名</label> <div class="controls"> <input type="text" id="q-customerName" name="customerName" class="form_value ac_input" autocomplete="off"> <input type="hidden" id="q-customerId" name="customerId" class="form_value" validate="n" value="0"><!-- 默認(rèn)值為0 --> </div> </div>
Javascipt代碼
$("#q-customerName").autocomplete("/CustomerName/getCustomerNameSuggestion.do",{ max:10,//最多5條記錄 minChars:1, scrollHeight: 250, width:206, dataType:'json',//返回的數(shù)據(jù)類型為JSON類型 extraParams: { "customerName": function () { return encodeURIComponent($("#q-customerName").val()); } }, parse:function(data) {//解釋返回的數(shù)據(jù),把其存在數(shù)組里 var parsed = []; for (var i = 0; i < data.length; i++) { parsed[parsed.length] = { data: data[i], key: data[i].key, value: data[i].value, result: data[i].value //返回的結(jié)果顯示內(nèi)容 }; } if (data.length == 0) { parsed.push({ data: { "key": "0", "value": "無(wú)搜索結(jié)果" }, key: "0", result: "無(wú)搜索結(jié)果" }); } return parsed; }, formatItem: function(item) {//顯示下拉列表的內(nèi)容 return item.value; }, formatMatch: function(item) { return item.value; }, formatResult: function(item) { return item.value; } }).result(function(event, item, formatted) {//把返回的結(jié)果內(nèi)容顯示在其他文本框上 $("#q-customerId").val(item.key); if(item.key == 0){ $("#q-customerName").val(""); } });
后端返回的json數(shù)據(jù)格式如下
[ { "key": "5133", "value": "上海**有限公司" }, { "key": "5197", "value": "上海**用品有限公司" }, { "key": "5202", "value": "上海**傳播有限公司" }, { "key": "5234", "value": "上海**用品有限公司" }, { "key": "5319", "value": "上海**用品有限公司" }, { "key": "5402", "value": "上海**用品有限公司" }, { "key": "5500", "value": "上海**有限公司" }, { "key": "5581", "value": "上海**用品有限公司" } ]
Firefox下有個(gè)小bug,使用搜狗輸入法輸入中文時(shí)不觸發(fā)antocomplete事件。正在解決。
測(cè)試發(fā)現(xiàn)JQuery1.9及以上不兼容,項(xiàng)目中使用的是1.8.3
這東西沒(méi)啥技術(shù)含量,就是麻煩,記錄一下避免以后在同樣的問(wèn)題上浪費(fèi)時(shí)間。
文章題目:JQueryAutocomplete實(shí)戰(zhàn)
轉(zhuǎn)載源于:http://chinadenli.net/article24/gshgce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、ChatGPT、微信公眾號(hào)、網(wǎng)站維護(hù)、自適應(yīng)網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)