As a developer, the search box is also an indispensable function when developing a website. Recently, I need to make a search box at work, similar to Baidu’s search box. In this article, we will share with you about the Jquery implementation of similar Baidu search box.
Need to achieve two functions:
1. Enter keywords and display the matching drop-down list
2. Select the matching item and find related content
This search bar is often used in general e-commerce websites. First, analyze the function implementation. Enter the keyword and immediately display the list of matching items. To implement this function, the input box needs to bind the "input" event, and then send an asynchronous request to the background. The data is displayed on the page. Use the mouse or the up and down keys to select matching items, and click Search or the "Enter" key to search for specific results. Two asynchronous requests are used here, the first requests the matching items, and the second requests the search results. Keyboard, mouse and input box events must be monitored. Flexibility must also be considered, that is, adapting to various similar needs. It is still difficult to do it well. The implementation is distributed as follows.
1.html and css
<body> <div id="search-form"></div> </body> <style type="text/css"> *{margin: 0;padding: 0;list-style:none;border:none;} body { font-family: "microsoft yahei" !important; background-color: #FDFFE0; } :focus { outline: none; } #search-form { position: relative; top: 50px; display: inline; } </style>
2. Import css and js files
Since the blog cannot upload files, you can go to my git: http://git.oschina There is a complete project file on .net/manliu.com/search_frame
3. Page reference js
<script type="text/javascript"> var proposals = ['百度1', '百度2', '百度3', '百度4', '百度5', '百度6', '百度7','17素材网','百度','新浪']; $(document).ready(function(){ $('#search-form').complete({ searchIn:function(val){//传入输入值,返回匹配值 /* var reg = /^[\u4F00-\u9FA5\uF900-\uFA2D]/; return reg.test(val); */ var word = "^"+val+".*"; var rs = []; $.each(proposals,function(i,n){ if(n.match(word)){ rs.push(n); } }); return rs; }, width:400, height: 30, submitIn: function(text){//搜索选定的值 alert(text); } }); }); </script>
The searchIn method here is used to return matching items, usually defining an asynchronous request inside, to The data is fetched in the background and an array is returned. For complex ones, the source code needs to be modified; submitIn is used to search for matching results, and generally can be requested asynchronously or synchronously.
The above content is a tutorial on query to implement a search box similar to Baidu. I hope it can help everyone.
Related recommendations:
Css to create a good-looking search box
How to use Js to implement the Baidu search box prompt function
JavaScript implements imitation Youku search box
The above is the detailed content of jquery implements a search box similar to Baidu. For more information, please follow other related articles on the PHP Chinese website!