JQuery搜索框自動補全(模糊匹配)功能實現(xiàn)示例
本地實現(xiàn)了一個搜索框自動補全的小功能,在JQuery UI的autocomplete插件的基礎上,加入了自己的業(yè)務代碼,貼出來回顧一下,同時可以給大家一個參考
首先貼出的是JQuery Ui 的自動補全插件部分的代碼,后面的功能都是在其基礎上追加的,直接拷貝到你的本地就可以直觀的看到運行效果,也可以到官網(wǎng)上面體驗和查看,為了方便,我這里是直接引入的JS鏈接點擊下載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ù)用戶的輸入,自動更換詞庫的形式
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$( "#tags" ).autocomplete({//調用補全功能
source: availableTags
});
} );
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
</body>
</html>
運行截圖

jquery-ui的自動補全功能截圖
下面說下我追加的部分功能及實現(xiàn)思路,有可以改進的地方還請指教:
1、首先,數(shù)據(jù)源要根據(jù)用戶輸入的內容實時更新.
輸入框的值隨著用戶的輸入會一直變動.所以,輸入框下方的推薦補全的內容要輸入的值進行變動,這里使用onkeyup屬性來監(jiān)聽鍵盤動作,并傳遞此時input的value值到js函數(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ù)庫中需要模糊檢索的字段都是中文的菜品名稱.所以,當用戶輸入字母的時候,進行了一下過濾,當輸入的內容中存在字母時,不提交給后臺處理
//字符串判斷函數(shù)
//判斷一個字符串是否混有字母,全中文返回true
function isChn(str) {
var reg = /^[\u4E00-\u9FA5]+$/;
if (!reg.test(str)) {
return false;
} else {
return true;
}
}
3、發(fā)現(xiàn)當字符串中含有空格的時候,上面的字符串判斷函數(shù),返回的內容不符合預期,然后加入了一個去除字符串中所有空格的功能
//去掉字符串中任意位置的空格,返回去除空格后的字符串
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、處理結束用戶的輸入后,就是提交到后臺,然后返回數(shù)據(jù)源了,也就是availableTags;這里我把availableTags聲明為全局變量.并且用同步的Ajax方式取回數(shù)據(jù),然后賦值給availableTags,然后在監(jiān)聽鍵盤的函數(shù)中,使用返回的數(shù)據(jù)調用自動補全功能.
//請求后端獲取數(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ù)
//捕捉鍵入的關鍵字
function catch_keyword(word = null) {
if (isChn(Trim(word, 'g'))) {//去掉空格后檢查字符串,如果符合,繼續(xù)請求后臺
get_source(word);
$("#tags").autocomplete({
source: availableTags //數(shù)據(jù)源
});
}
}
到這里,這個功能已經(jīng)基本結束了,在測試過程中發(fā)現(xiàn)了一個小問題,每次第一次獲取用戶輸入的時候,自動補全功能沒有觸發(fā),在用戶繼續(xù)輸入后,才觸發(fā)成功,經(jīng)過調試,我在頁面加載完成后,初始化一下自動補全插件,解決了這個問題
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ù)源
//先初始化自動補全功能
$("#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;
}
}
//捕捉鍵入的關鍵字
function catch_keyword(word = null) {
if (isChn(Trim(word, 'g'))) {
get_source(word);
$("#tags").autocomplete({
source: availableTags //數(shù)據(jù)源
});
}
}
//請求后端獲取數(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>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
jQuery Datatable 多個查詢條件自定義提交事件(推薦)
這篇文章主要介紹了jQuery Datatable 多個查詢條件自定義提交事件的相關資料,需要的朋友可以參考下2017-08-08
jQuery實現(xiàn)數(shù)秒后自動提交form的方法
這篇文章主要介紹了jQuery實現(xiàn)數(shù)秒后自動提交form的方法,實例分析了jQuery實現(xiàn)form表單延時提交的技巧,需要的朋友可以參考下2015-03-03

