Home >Web Front-end >JS Tutorial >jquery.fastLiveFilter.js method to implement automatic input filtering_jquery
The example in this article describes how jquery.fastLiveFilter.js implements automatic input filtering. Share it with everyone for your reference. The details are as follows:
This effect is achieved using the jquery.fastLiveFilter.js plug-in. It is similar to the input prompt function of the search box and implements the automatic filtering function of matching items. When you input, it will intelligently match matching items based on the entered characters. The content is automatically listed to improve the humanized operation experience. If you are interested in the usage of jquery.fastLiveFilter.js plug-in, this is a very good example.
The screenshot of the running effect is as follows:
The specific code is as follows:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>jQuery过滤器插件fastLiveFilter</title> <script src="jquery-1.6.2.min.js"></script> <script> //jquery.fastLiveFilter.js jQuery.fn.fastLiveFilter = function(list, options) { options = options || {}; list = jQuery(list); var input = this; var lastFilter = ''; var timeout = options.timeout || 0; var callback = options.callback || function() {}; var keyTimeout; var lis = list.children(); var len = lis.length; var oldDisplay = len > 0 ? lis[0].style.display : "block"; callback(len); input.change(function() { var filter = input.val().toLowerCase(); var li, innerText; var numShown = 0; for (var i = 0; i < len; i++) { li = lis[i]; innerText = !options.selector ? (li.textContent || li.innerText || "") : $(li).find(options.selector).text(); if (innerText.toLowerCase().indexOf(filter) >= 0) { if (li.style.display == "none") { li.style.display = oldDisplay; } numShown++; } else { if (li.style.display != "none") { li.style.display = "none"; } } } callback(numShown); return false; }).keydown(function() { clearTimeout(keyTimeout); keyTimeout = setTimeout(function() { if( input.val() === lastFilter ) return; lastFilter = input.val(); input.change(); }, timeout); }); return this; } </script> <script> $(function() { $('#search_input').fastLiveFilter('#search_list'); }); </script> <style type="text/css"> body { margin: 0px; background-color: #F6F6F6; } .jq22{ width: 600px; height: 500px; margin-left: auto; margin-right: auto; background-color: #FFFFFF; padding: 10px; } </style> </head> <body> <div class="jq22"> <input id="search_input" placeholder="输入文字开始筛选"> <ul id="search_list"> <li>One</li> <li>Two</li> <li>Three</li> <li>One</li> <li>awo</li> <li>bhree</li> <li>cne</li> <li>dwo</li> <li>ehree</li> <li>fne</li> <li>gwo</li> <li>hhree</li> <li>ihree</li> </ul> </div> </body> </html>
I hope this article will be helpful to everyone’s jquery programming design.