


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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
