本文實例為大家分享了Jquery搜索關鍵字自動匹配功能的實現代碼,供大家參考,具體內容如下
jQuery AutoComplete 是一個基于jQuery實現搜索關鍵字自動匹配提示的插件,該插件可擴展性強,表現性能優越,方便整合到自己的項目中使用;兼容IE 6.0+, FF 2+, Safari 2.0+, Opera 9.0+, and Chrome 1.0+ 等主流瀏覽器。
下面是具體的使用方法:
1、使用設置
首頁,要把插件的js代碼嵌入到你自己的項目中去。
<script src="http://www.gimoo.net/t/1903/jquery.js" type="text/javascript"><mce:0--></script><script src="http://www.gimoo.net/t/1903/jquery.autocomplete.js" type="text/javascript"><mce:1--></script>
2、使用方法
為要實現自動匹配提示的 input 表單添加 AutoComplete 功能。
<input id="query" name="q" />
初始化 AutoComplete 對象,確保正確加載 DOM 對象,否則IE下的用戶可能會出現錯誤。
$('#query').autocomplete({ serviceUrl: 'service/autocomplete.ashx', // Page for processing autocomplete requests minChars: 2, // Minimum request length for triggering autocomplete delimiter: /(,|;)s*/, // Delimiter for separating requests (a character or regex) maxHeight: 400, // Maximum height of the suggestion list, in pixels width: 300, // List width zIndex: 9999, // List's z-index deferRequestBy: 0, // Request delay (milliseconds), if you prefer not to send lots of requests while the user is typing. I usually set the delay at 300 ms. params: { country: 'Yes'}, // Additional parameters onSelect: function(data, value){ }, // Callback function, triggered if one of the suggested options is selected, lookup: ['January', 'February', 'March'] // List of suggestions for local autocomplete });
根據文本表單中的輸入信息,進行關鍵字提示匹配。
{ query:'Li', // Original request suggestions:['Liberia','Libyan Arab Jamahiriya','Liechtenstein','Lithuania'], // List of suggestions data:['LR','LY','LI','LT'] // Optional parameter: list of keys for suggestion options; used in callback functions. }
jQuery AutoComplete 插件支持 on/off功能,從而控制效果的開關。
var ac = $('#query').autocomplete({ /*parameters*/ }); ac.disable(); ac.enable(); ac.setOptons({ zIndex: 1001 });
3、設置表現樣式
最后,用div和css美化表現效果。
<div class="autocomplete-w1"><div id="Autocomplete_1240430421731" class="autocomplete" style="width: 299px;"><div><strong>Li</strong>beria</div><div><strong>Li</strong>byan Arab Jamahiriya</div><div><strong>Li</strong>echtenstein</div><div class="selected"><strong>Li</strong>thuania</div></div></div> .autocomplete-w1 { background:url(img/shadow.png) no-repeat bottom right; position:absolute; top:0px; left:0px; margin:6px 0 0 6px; /* IE6 fix: */ _background:none; _margin:1px 0 0 0; }.autocomplete { border:1px solid #999; background:#FFF; cursor:default; text-align:left; max-height:350px; overflow:auto; margin:-6px 6px 6px -6px; /* IE6 specific: */ _height:350px; _margin:0; _overflow-x:hidden; }.autocomplete .selected { background:#F0F0F0; }.autocomplete div { padding:2px 5px; white-space:nowrap; overflow:hidden; }.autocomplete strong { font-weight:normal; color:#3399FF; }
4、實例講解
<html> <head> <title></title> <style type="text/css"> #txtKey{ width:300px;} </style> <link rel="stylesheet" type="text/css" /> <script src="http://www.gimoo.net/t/1903/Jquery1.7.js" type="text/javascript"></script> <script src="http://www.gimoo.net/t/1903/js/jquery.autocomplete.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { var array = ['asp.net', 'asp.net mvc', 'wcf', 'wpf', 'win8', 'windows phone', '張東', '張熙', '張亞飛']; /*autocomplete函數 (1)獲取txtKey中用戶輸入的值(用戶每輸入一個字符,都會獲取一次) (2)將獲取的值和array集合中的元素進行比較,找出匹配的元素,并顯示出來 (3)會將用戶選擇的項添加到txtKey中*/ /*result函數:對用戶選擇的結果進行操作。data參數表示用戶選擇的項*/ $('#txtKey').autocomplete(array).result(function (event, data) { window.location. + data + '&rsv_bp=0&ch=&tn=baidu&bar=&rsv_spt=3&ie=utf-8&rsv_sug3=6&rsv_sug=0&rsv_sug1=3&rsv_sug4=229&inputT=1458'; }) }) </script> </head> <body> <input id="txtKey" type="text" /><input id="Button1" type="button" value="百度一下" /> <input id="Text1" type="text" /> </body> </html>
實現效果如下:
以上就是關于jQuery AutoComplete使用方法介紹,通過完整示例為大家展示jQuery AutoComplete使用效果,希望對大家的學習有所幫助。