小編給大家分享一下Ajax如何實(shí)現(xiàn)模擬關(guān)鍵字智能匹配搜索效果,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)咨詢熱線:13518219792,為您提供成都網(wǎng)站建設(shè)網(wǎng)頁(yè)設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),創(chuàng)新互聯(lián)網(wǎng)頁(yè)制作領(lǐng)域10年,包括砂巖浮雕等多個(gè)行業(yè)擁有豐富的網(wǎng)站推廣經(jīng)驗(yàn),選擇創(chuàng)新互聯(lián),為網(wǎng)站保駕護(hù)航!
準(zhǔn)備數(shù)據(jù)keyword.json:(這里只貼出部分?jǐn)?shù)據(jù))
[ {"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(進(jìn)口)"}, {"id":7,"initial":"ada8","keyword":"奧迪A8L(進(jìn)口)"}, {"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結(jié)構(gòu)
js:
//當(dāng)在搜索框輸入內(nèi)容時(shí),根據(jù)關(guān)鍵字匹配,顯示彈出層 function searchSuggest(obj){ var searchKey=$(obj).val(); var reg = new RegExp(searchKey,"i"); //忽略大小寫(xiě)匹配搜索框中輸入的內(nèi)容 $.ajax({ type:"get", url:"data/keyword.json", dataType:"json", success:function(data){ var arr=[]; for(var i=0,len=data.length;i"+data[i].keyword+""); } } $(".keywords_list").html(arr).show(); } }); } //單擊匹配列表中的關(guān)鍵字選項(xiàng)時(shí),將該關(guān)鍵字顯示在搜索框中 function changeSearchKey(obj){ var value=$(obj).text(); $("#searchKey").val(value); $('.keywords_list').hide(); }
效果圖:
ajax是一種在無(wú)需重新加載整個(gè)網(wǎng)頁(yè)的情況下,能夠更新部分網(wǎng)頁(yè)的技術(shù),可以通過(guò)在后臺(tái)與服務(wù)器進(jìn)行少量數(shù)據(jù)交換,使網(wǎng)頁(yè)實(shí)現(xiàn)異步更新。
以上是“Ajax如何實(shí)現(xiàn)模擬關(guān)鍵字智能匹配搜索效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!