Ajax實現類比關鍵字智能匹配搜尋效果執行個體教程

來源:互聯網
上載者:User
本文主要介紹了Ajax實現類比關鍵字智能匹配搜尋效果,當在搜尋方塊輸入內容時,根據關鍵字匹配,顯示彈出層,非常不錯,具有參考借鑒價值,需要的朋友可以參考下,希望能協助到大家。

準備資料keyword.json:(這裡只貼出部分資料)


[  {"id":1,"initial":"ad","keyword":"奧迪"},  {"id":2,"initial":"ada4l","keyword":"奧迪A4L"},  {"id":3,"initial":"ada6l","keyword":"奧迪A6L"},  {"id":4,"initial":"adq5","keyword":"奧迪Q5"},  {"id":5,"initial":"ada3","keyword":"奧迪A3"},  {"id":6,"initial":"adq7","keyword":"奧迪Q7(進口)"},  {"id":7,"initial":"ada8","keyword":"奧迪A8L(進口)"},  {"id":8,"initial":"bm","keyword":"寶馬"},  {"id":9,"initial":"bm5x","keyword":"寶馬5系"},  {"id":10,"initial":"bm7x","keyword":"寶馬7系"},  {"id":11,"initial":"bt","keyword":"本田"},  {"id":12,"initial":"bqsbx25","keyword":"北汽紳寶 X25"},  {"id":13,"initial":"bqsbx35","keyword":"北汽紳寶X35"},  {"id":14,"initial":"bqsbx55","keyword":"北汽紳寶X55"} ]

html結構


<form class="fl search_form" action="#" method="post">  <input class="search_text" id="searchKey" type="search" placeholder="請輸入搜尋索引鍵" onkeyup="searchSuggest(this);"/>  <input class="search_btn" type="submit" value="搜尋"/> </form> <!--start--智能搜尋索引鍵匹配彈出層--> <ul class="keywords_list"></ul> <!--end--智能搜尋索引鍵匹配彈出層-->

js:


//當在搜尋方塊輸入內容時,根據關鍵字匹配,顯示彈出層 function searchSuggest(obj){  var searchKey=$(obj).val();  var reg = new RegExp(searchKey,"i"); //忽略大小寫匹配搜尋方塊中輸入的內容  $.ajax({  type:"get",  url:"data/keyword.json",  dataType:"json",  success:function(data){   var arr=[];   for(var i=0,len=data.length;i<len;i++){   if(searchKey!="" && (data[i].initial.search(reg)!=-1 || data[i].keyword.search(reg)!=-1)) {    arr.push("<li onclick='changeSearchKey(this);'>"+data[i].keyword+"</li>");   }   }       $(".keywords_list").html(arr).show();  }  }); } //單擊匹配列表中的關鍵字選項時,將該關鍵字顯示在搜尋方塊中 function changeSearchKey(obj){  var value=$(obj).text();  $("#searchKey").val(value);  $('.keywords_list').hide(); }


相關推薦:

JavaScript之replace實現搜尋索引鍵高亮顯示方法

ES6 Class 中的 super 關鍵字詳解

jq、ajax、php、mysql實現關鍵字模糊查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.