Home >Web Front-end >JS Tutorial >Implementing automatic search keyword matching function based on jQuery_jquery
Today we will look at a simple example of automatic keyword matching based on jquery. I hope the article will be helpful to you.
Example 1
In the project, the user is sometimes required to select a city, but there are too many cities and it is inconvenient for the user to choose. Therefore, an input box is provided where the user can enter the Chinese characters or pinyin abbreviation of the city. The result diagram is as follows:
The result after inputting pinyin is as follows:
The implementation code is as follows:
<html><head><title>实时查询城市通过姓名或拼音简写</title></head><meta charset = "utf-8" ><script type="text/javascript" src="jquery.min.js"></script><body><input id="searchCityName" style="width: 100%;" type="text" placeholder="中文 / 拼音首字母" /> <ul> <li pinyin="bj" cityname="北京"><a href="/cityBranch/12.html">北京 </a></li> <li pinyin="dl" cityname="大连"><a href="/cityBranch/14.html">大连 </a></li> <li pinyin="sh" cityname="上海"><a href="/cityBranch/13.html">上海 </a></li> <li pinyin="jn" cityname="济南"><a href="/cityBranch/15.html">济南 </a></li> <li pinyin="gz" cityname="广州"><a href="/cityBranch/17.html">广州 </a></li> <li pinyin="jh" cityname="金华"><a href="/cityBranch/18.html">金华 </a></li> <li pinyin="wh" cityname="武汉"><a href="/cityBranch/19.html">武汉 </a></li> <li pinyin="nj" cityname="南京"><a href="/cityBranch/20.html">南京 </a></li> <li pinyin="sz" cityname="深圳"><a href="/cityBranch/22.html">深圳 </a></li> <li pinyin="tj" cityname="天津"><a href="/cityBranch/21.html">天津 </a></li> <li pinyin="cd" cityname="成都"><a href="/cityBranch/24.html">成都 </a></li> <li pinyin="ly" cityname="临沂"><a href="/cityBranch/25.html">临沂 </a></li> <li pinyin="cc" cityname="长春"><a href="/cityBranch/26.html">长春 </a></li> <li pinyin="hz" cityname="杭州"><a href="/cityBranch/27.html">杭州 </a></li> <li pinyin="nb" cityname="宁波"><a href="/cityBranch/28.html">宁波 </a></li> <li pinyin="qd" cityname="青岛"><a href="/cityBranch/29.html">青岛 </a></li> <li pinyin="sy" cityname="沈阳"><a href="/cityBranch/33.html">沈阳 </a></li> </ul> <script> function searchCity() { var searchCityName = $("#searchCityName").val(); if (searchCityName == "") { $("ul li").show(); } else { $("ul li").each( function() { var pinyin = $(this).attr("pinyin"); var cityName = $(this).attr("cityName"); if (pinyin.indexOf(searchCityName) != -1 || cityName.indexOf(searchCityName) != -1) { $(this).show(); } else { $(this).hide(); } }); } } $('#searchCityName').bind('input propertychange', function() { searchCity(); }); </script></body></html>
Note:
1. When I want to query the list value in the input box in real time, the first solution that comes to mind is to use ajax, but after thinking about it, I found that the value of the list is basically fixed. Why not load it all at once, so The background code was changed to load all city details.
2. When the value in the input box changes, an event needs to be triggered. My first idea was to use onchange, but in fact onchange means that the value of the input box changes and the input box loses focus, so I finally used keyup. There is no problem with keyup when tested on the computer, but on WeChat, it does not take effect. So keyup was replaced with the final bind('input propertychange', function() {} .
3. When judging whether the city characters include the characters in the input box, I used the contains function. There was no problem when testing under Firefox, but it did not take effect on Chrome and WeChat clients. Finally, contains is replaced with indexOf.
Example 2, use jquery.autocomplete plug-in to implement.
1. Use settings
Home page, you need to embed the plugin’s js code into your own project.
<script src="jquery.js" type="text/javascript"><!--mce:0--></script><script src="jquery.autocomplete.js" type="text/javascript"><!--mce:1--></script>
2. How to use
Add the AutoComplete function to the input form where automatic matching prompts are to be implemented.
<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 });
Perform keyword prompt matching based on the input information in the text form.
{ 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. Set the performance style
Finally, use div and css to beautify the performance effect.
<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; } jQuery AutoComplete
The two examples shared above are all about jQuery’s automatic search keyword matching function. I hope it will be helpful to everyone’s learning.