html5 模糊匹配搜索框


使用bootstrap3-typeahead.js

文件在這里

引用:

 <script type="text/javascript" src="@Url.Content("XXXXX/bootstrap3-typeahead.js")" charset="UTF-8"></script>

input控件

<input id="local_data" autocomplete="off" data-provide="typeahead" type="text" style="background: white"
                               class="input-sm form-control" placeholder="輸入關鍵字以搜索" />

JS樣例代碼

var initSearchLabel = function () {
    var resultKeyAndValue = {};
    $("#local_data").typeahead({
        source: function (query, process) {
            //query是輸入的值
            var names = new Array();
            $.ajax({
                type: "GET",
                url: "XXXXXXXX",
                datatype: "json",
                contentType: "application/json; charset=utf-8",
                cache: false,
                traditional: true,
                async: false,
                success: function (result) {
                    result = JSON.parse(result);
                   //處理得到的JSON數據,var resultList = JSON.parse(result);
           //如果得到的數據是對象數組而不是字符串數組,將要顯示的搜索結果處理為一個字符串數組並返回 ;
           //如果在選中以后的處理方法中還需要被選中對象的其他參數,可以將選中的字符串與使用的其他參數做成一個Dictionary備用
for (var i = 0; i < resultList.length; i++) { names.push(resultList[i].NAME); } return process(names); }, error: function (result) { } }); }, updater: function (item) {
      //選中以后的數據處理。item為選中的字符串,resultKeyAndValue為success函數紅提到的備用Dictionary,在此取出需要用到的參數。
var info = resultKeyAndValue[item];var name = info["name"]; $.ajax({ type: "GET", url: "YYYYYYYYYYYYYYY" , datatype: "json", contentType: "application/json; charset=utf-8", cache: false, traditional: true, async: false, success: function (result) { result = JSON.parse(result); }, error: function (result) { } }); return item; }, items: 100,//顯示的搜索結果 delay: 500 //input控件中數據發生變化以后執行請求的間隔 }); }

該方法匹配的是完整字符串,還達不到百度輸入框中的 多個關鍵字匹配方式


免責聲明!

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



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