使用bootstrap typeahead輸入框自動補全之 問題與解決


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

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

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 是配備的關鍵字,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);//這必須有,要不還是顯示為underfined
                                    for(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 info
                                            arr.push({ value:str});
                                        }
                                        
                                    }
                                    process(arr);
                                    
                            }
                        }
                    })
                    
         }
        }
         $('input.typeahead').typeahead({
            hint: true,
            highlight: true,
            minLength: 1
         }, {
            name: 'states',
            displayKey: 'value',
            source: substringMatcher()//當前版本source屬性中不能直接調用ajax方法獲取數據源,通過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="機構名稱" class="col-xs-10 col-sm-5 typeahead scrollable" value="" autocomplete="off" />

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM