国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

使用bootstrap typeahead插件實(shí)現(xiàn)輸入框自動(dòng)補(bǔ)全之問題及解決辦法

2019-11-20 09:32:32
字體:
供稿:網(wǎng)友

根據(jù)網(wǎng)上查找到的 typeahead使用方法,到最后一步時(shí)就出錯(cuò),數(shù)據(jù)能從數(shù)據(jù)庫(kù)讀取出來,但在輸入框顯示提示時(shí),全都顯為:underfined。捉摸了半天都發(fā)現(xiàn)不了問題出在哪兒。后來在http://blog.64cm.com/post/2014/08/13/%E4%BD%BF%E7%94%A8bootstrap-typeahead%E6%8F%92%E4%BB%B6 上不經(jīng)意發(fā)現(xiàn)這么一句話:“在當(dāng)前版本的typeahead中,已經(jīng)不再支持在source屬性中直接調(diào)用ajax方法獲取數(shù)據(jù)源了。”提醒了我,因?yàn)槲腋鶕?jù)網(wǎng)上的方法,是直接在source中調(diào)用ajax方法。

再回頭現(xiàn)在的ace demo,雖然沒有調(diào)用ajax的示例,但也有注釋說明如何用,只不過用的是英文(題外話:做技術(shù)的懂英語真的很重要。),經(jīng)過一翻調(diào)試,終于能正確顯示了。貼出代碼如下:

js代碼

<script type="text/javascript">jQuery(function($) {//typeahead.js//example taken from plugin's page at: https://twitter.github.io/typeahead.js/examples/var substringMatcher = function() {//substringMatcher()方法return function findMatches(query, process) {//query 是配備的關(guān)鍵字,processj是返回的值var matches, substringRegex;var params = {"token": getStorage("token"), "flag":0,"name":query};var parameter_str="";for(var key in params){ parameter_str+="&"+key+"="+params[key];} var fullurl=getOption("gykj_host")+"institution/list?"+getOption("gykj_callbackparam")+"="+getOption("gykj_callbackfunc")+parameter_str;$("#submenu_info").html(fullurl);$.ajax({url:fullurl,type:'get',dataType:"jsonp",jsonp:getOption("gykj_callbackparam"),jsonpCallback:getOption("gykj_callbackfunc"),async:false,error:function(){alert("列表:"+getOption("connectionErrorMessage"));},success:function(data){//$("#submenu_info").html(fullurl);if(data.code==0){var arr,substringRegex;arr=[];substrRegex = new RegExp(query);//這必須有,要不還是顯示為underfinedfor(var item in data.data){var str= data.data[item].name;if (substrRegex.test(str)) {// the typeahead jQuery plugin expects suggestions to a// JavaScript object, refer to typeahead docs for more infoarr.push({ value:str});}}process(arr);}}})}}$('input.typeahead').typeahead({hint: true,highlight: true,minLength: 1}, {name: 'states',displayKey: 'value',source: substringMatcher()//當(dāng)前版本source屬性中不能直接調(diào)用ajax方法獲取數(shù)據(jù)源,通過substringMatcher()方法});});</script> 

html

<!-- inline scripts related to this page --><script src="../assets/js/ace-elements.js"></script><script src="../assets/js/typeahead.jquery.js"></script><input type="text" id="name" placeholder="機(jī)構(gòu)名稱" class="col-xs-10 col-sm-5 typeahead scrollable" value="" autocomplete="off" />

以上所述是小編給大家介紹的使用bootstrap typeahead插件實(shí)現(xiàn)輸入框自動(dòng)補(bǔ)全之問題及解決辦法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 中方县| 安泽县| 新巴尔虎左旗| 遂川县| 泸溪县| 化州市| 敦煌市| 余江县| 金阳县| 探索| 五指山市| 隆子县| 定边县| 灵川县| 澄城县| 宁陕县| 多伦县| 铜川市| 水城县| 玉环县| 德江县| 海城市| 丹寨县| 平昌县| 三原县| 两当县| 遂川县| 葫芦岛市| 西平县| 社旗县| 东海县| 宝坻区| 马公市| 合水县| 洞头县| 盘锦市| 阳信县| 那坡县| 雷波县| 原平市| 建瓯市|