JQuery搜索框自動(dòng)補(bǔ)全(模糊匹配)功能實(shí)現(xiàn)示例
本地實(shí)現(xiàn)了一個(gè)搜索框自動(dòng)補(bǔ)全的小功能,在JQuery UI的autocomplete插件的基礎(chǔ)上,加入了自己的業(yè)務(wù)代碼,貼出來回顧一下,同時(shí)可以給大家一個(gè)參考
首先貼出的是JQuery Ui 的自動(dòng)補(bǔ)全插件部分的代碼,后面的功能都是在其基礎(chǔ)上追加的,直接拷貝到你的本地就可以直觀的看到運(yùn)行效果,也可以到官網(wǎng)上面體驗(yàn)和查看,為了方便,我這里是直接引入的JS鏈接點(diǎn)擊下載JQuery UI的源碼
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>jQuery UI Autocomplete - Default functionality</title> <link rel="stylesheet" rel="external nofollow" rel="external nofollow" > <link rel="stylesheet" href="/resources/demos/style.css" rel="external nofollow" > <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script> $( function() { var availableTags = [//這里要改成根據(jù)用戶的輸入,自動(dòng)更換詞庫的形式 "ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme" ]; $( "#tags" ).autocomplete({//調(diào)用補(bǔ)全功能 source: availableTags }); } ); </script> </head> <body> <div class="ui-widget"> <label for="tags">Tags: </label> <input id="tags"> </div> </body> </html>
運(yùn)行截圖
jquery-ui的自動(dòng)補(bǔ)全功能截圖
下面說下我追加的部分功能及實(shí)現(xiàn)思路,有可以改進(jìn)的地方還請(qǐng)指教:
1、首先,數(shù)據(jù)源要根據(jù)用戶輸入的內(nèi)容實(shí)時(shí)更新.
輸入框的值隨著用戶的輸入會(huì)一直變動(dòng).所以,輸入框下方的推薦補(bǔ)全的內(nèi)容要輸入的值進(jìn)行變動(dòng),這里使用onkeyup屬性來監(jiān)聽鍵盤動(dòng)作,并傳遞此時(shí)input的value值到j(luò)s函數(shù)中.
//html <input type="search" class="" id="tags" placeholder="搜索" required="" onkeyup="catch_keyword(this.value)"> //js代碼 function catch_keyword(word) {//這里接受并log出value console.log(word); }
2、第2步,考慮到數(shù)據(jù)庫中需要模糊檢索的字段都是中文的菜品名稱.所以,當(dāng)用戶輸入字母的時(shí)候,進(jìn)行了一下過濾,當(dāng)輸入的內(nèi)容中存在字母時(shí),不提交給后臺(tái)處理
//字符串判斷函數(shù) //判斷一個(gè)字符串是否混有字母,全中文返回true function isChn(str) { var reg = /^[\u4E00-\u9FA5]+$/; if (!reg.test(str)) { return false; } else { return true; } }
3、發(fā)現(xiàn)當(dāng)字符串中含有空格的時(shí)候,上面的字符串判斷函數(shù),返回的內(nèi)容不符合預(yù)期,然后加入了一個(gè)去除字符串中所有空格的功能
//去掉字符串中任意位置的空格,返回去除空格后的字符串 function Trim(str, is_global) { var result; result = str.replace(/(^\s+)|(\s+$)/g, ""); if (is_global.toLowerCase() == "g") { result = result.replace(/\s/g, ""); } return result; }
4、處理結(jié)束用戶的輸入后,就是提交到后臺(tái),然后返回?cái)?shù)據(jù)源了,也就是availableTags;這里我把a(bǔ)vailableTags聲明為全局變量.并且用同步的Ajax方式取回?cái)?shù)據(jù),然后賦值給availableTags,然后在監(jiān)聽鍵盤的函數(shù)中,使用返回的數(shù)據(jù)調(diào)用自動(dòng)補(bǔ)全功能.
//請(qǐng)求后端獲取數(shù)據(jù)源 function get_source(word = null) { var url = "<?php echo base_url('admin/Demo/source');?>?keyword=" + word; $.get({ type: 'GET', url: url, async: false,//改為同步 dataType: 'json', success: function (response) { console.log('1'); availableTags = response; } }); }
這里更新下最開始的接收監(jiān)聽鍵盤后的value值的函數(shù)
//捕捉鍵入的關(guān)鍵字 function catch_keyword(word = null) { if (isChn(Trim(word, 'g'))) {//去掉空格后檢查字符串,如果符合,繼續(xù)請(qǐng)求后臺(tái) get_source(word); $("#tags").autocomplete({ source: availableTags //數(shù)據(jù)源 }); } }
到這里,這個(gè)功能已經(jīng)基本結(jié)束了,在測(cè)試過程中發(fā)現(xiàn)了一個(gè)小問題,每次第一次獲取用戶輸入的時(shí)候,自動(dòng)補(bǔ)全功能沒有觸發(fā),在用戶繼續(xù)輸入后,才觸發(fā)成功,經(jīng)過調(diào)試,我在頁面加載完成后,初始化一下自動(dòng)補(bǔ)全插件,解決了這個(gè)問題
6. 附:完整代碼
不知道如何在markdown中添加下載鏈接,只好把完整代碼放上來啦!
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title></title> <link rel="stylesheet" href="/jquery-weui-build/dist/lib/weui.min.css" rel="external nofollow" > <link rel="stylesheet" href="/jquery-weui-build/dist/css/jquery-weui.css" rel="external nofollow" > <link rel="stylesheet" href="/jquery-weui-build/demos/css/demos.css" rel="external nofollow" > <link rel="stylesheet" rel="external nofollow" rel="external nofollow" > <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="/static/jquery-weui-build/dist/lib/fastclick.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script> $(function () { FastClick.attach(document.body); }); </script> <script src="/jquery-weui-build/dist/js/jquery-weui.js"></script> </head> <body> <div class="ui-widget"> <div class="weui-search-bar" id="searchBar"> <form class="weui-search-bar__form" action="#"> <div class="weui-search-bar__box"> <i class="weui-icon-search"></i> <input type="search" class="weui-search-bar__input" id="tags" placeholder="搜索" required="" onkeyup="catch_keyword(this.value)"> <a href="javascript:" rel="external nofollow" rel="external nofollow" class="weui-icon-clear" id="searchClear"></a> </div> <label class="weui-search-bar__label" id="searchText" style="transform-origin: 0px 0px 0px; opacity: 1; transform: scale(1, 1);"> <i class="weui-icon-search"></i> <span>搜索</span> </label> </form> <a href="javascript:" rel="external nofollow" rel="external nofollow" class="weui-search-bar__cancel-btn" id="searchCancel">取消</a> </div> </div> <script> var availableTags = [];//數(shù)據(jù)源 //先初始化自動(dòng)補(bǔ)全功能 $("#tags").autocomplete({ source: availableTags //數(shù)據(jù)源 }); //去掉字符串中任意位置的空格 function Trim(str, is_global) { var result; result = str.replace(/(^\s+)|(\s+$)/g, ""); if (is_global.toLowerCase() == "g") { result = result.replace(/\s/g, ""); } return result; } //判斷字符串是否全是中文 function isChn(str) { var reg = /^[\u4E00-\u9FA5]+$/; if (!reg.test(str)) { return false; } else { return true; } } //捕捉鍵入的關(guān)鍵字 function catch_keyword(word = null) { if (isChn(Trim(word, 'g'))) { get_source(word); $("#tags").autocomplete({ source: availableTags //數(shù)據(jù)源 }); } } //請(qǐng)求后端獲取數(shù)據(jù)源 function get_source(word = null) { var url = "<?php echo base_url('admin/Demo/source');?>?keyword=" + word; $.get({ type: 'GET', url: url, async: false,//改為同步 dataType: 'json', success: function (response) { console.log('1'); availableTags = response; } }); } </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery Datatable 多個(gè)查詢條件自定義提交事件(推薦)
這篇文章主要介紹了jQuery Datatable 多個(gè)查詢條件自定義提交事件的相關(guān)資料,需要的朋友可以參考下2017-08-08jQuery獲取標(biāo)簽文本內(nèi)容和html內(nèi)容的方法
這篇文章主要介紹了jQuery獲取標(biāo)簽文本內(nèi)容和html內(nèi)容的方法,實(shí)例分析了jQuery中text和html方法的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03JQuery實(shí)現(xiàn)ajax請(qǐng)求的示例和注意事項(xiàng)
今天小編就為大家分享一篇關(guān)于JQuery實(shí)現(xiàn)ajax請(qǐng)求的示例和注意事項(xiàng),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12jQuery實(shí)現(xiàn)數(shù)秒后自動(dòng)提交form的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)數(shù)秒后自動(dòng)提交form的方法,實(shí)例分析了jQuery實(shí)現(xiàn)form表單延時(shí)提交的技巧,需要的朋友可以參考下2015-03-03jQuery中創(chuàng)建實(shí)例與原型繼承揭秘
在普通情況下我們要用原生類、或者自定義類創(chuàng)建實(shí)例要用new運(yùn)算符,使構(gòu)造函數(shù)創(chuàng)建一個(gè)實(shí)例,并且實(shí)例繼承構(gòu)造器prototype上的所有公有方法2011-12-12最常見的左側(cè)分類菜單欄jQuery實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了最常見的左側(cè)分類菜單欄jQuery實(shí)現(xiàn)代碼,仿京東、淘寶等各大類網(wǎng)站效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11