Front-end JS fuzzy search (fuzzy query)

Source: Internet
Author: User

1.html structure:

<label for= "Searchshop" class= "clear Pos-a" style= "TOP:17PX;" >
<input type= "text" id= "Searchshop" placeholder= "Site Search" >
<input type= "button" value= "Search" class= "Searchicon searchshopbtn" >
</label>

/* Query results put UL inside * *

<ul id= "SearchResult" class= "SearchResult" >

</ul>

2.CSS Style:

#searchShop {

  1. Line-height: 28px;
  2. Text-indent: 5px;
  3. Width: 180px;
  4. Float:left
  5. Height: 28px;
  6. border: none;
}. searchshopbtn{
  1. Font-size: 0;
  2. padding-left: 15px;
  3. padding-right: 15px;
  4. background-color: #eff3f6;
  5. background-repeat: no-repeat;
  6. background-position: 8px 5px;
}. Searchicon {//Small search icon
    1. Background-image: url (data:image/png;base64,ivborw0kggoaaaansuheugaaabiaaaascayaaabwzo5xaaaagxrfw ... 07gdgaip9c+y2upkm1qjwixb+hwqagjnqqj5gyqaqaagwakpg6e/xofymaaaaasuvork5cyii=);
}/ * display box CSS for query results* /. SearchResult {
  1. Position: absolute;
  2. Top: 47px;
  3. Right : 70px;
  4. width: 180px;
  5. border: 1px solid #e4e7ee;
  6. border-top: 0;
  7. Border-bottom: 0;
  8. Background: #fff;
  9. max-height: 279px;
  10. overflow-y: auto;
  11. overflow-x: hidden;
  12. z-index: 2;
}/ * list is all Li composition css*/. SearchResult Li {
  1. border-bottom: 1px solid #e4e7ee;
  2. line-height: 30px;
  3. padding-left: 4px;
  4. width: 176px;
  5. List-style:none;
}/ * Above is style, not suitable please adjust * /The static effect is this:If you enter a C: Then the result is this.The corresponding structure of the search results is this: the inside of the Li is added dynamically, just need to put this UL write well put on the line.   3. Search function implementation of the relevant JS, dependent on Jquery.js
var ishasnextpage = false;//Whether there is a next page of data
var searchitem = ";//Search box Contents
var pagenum = 1;//Default page

Listen to the search box input event, the content of the words to query
$ (' #searchShop '). On (' Input ', function () {
var _v = $.trim ($ (this). Val ());
$ (' #searchResult '). empty ();
Pagenum = 1;
Ishasnextpage = false;
if (_v! = ") {
Searchitem = _v;
SEARCHSHOPFN (_v, pagenum);
}
});
/**
* search results drop-down load more, each page shows 10, if the search results more than 10, dropdown corresponding list, will automatically add additional data in
* @method nextload ()
* @param {nextload:false or Ture,keyword:keyword,nextload:true or False}
* @return {data}
*/

function Nextload (KeyWord, Ishasnextpage) {
if (!! Ishasnextpage) {
Searchshopfn (keyWord, pagenum + = 1)
}
}

//Scroll to the bottom to load data
$ (' #searchResult '). Scroll ( function () {
if ($ (this). "Height () + $ (this). ScrollTop () >= $ (this) [0].scrollheight] {
Nextload (Searchitem, is Hasnextpage);
}
})
Click each piece of data to assign a value to the search box, and you need to reset the corresponding parameters
$ (document). On (' click ', '. SearchResult li ', function () {
var _v = $ (this). text ();
var _shopid = $ (this). attr (' shopid ');
$ (' #searchShop '). Val (_v). attr (' Shopid ', _shopid);
$ (' #searchResult '). Hide ();
Ishasnextpage = false;
return false;

});

Fuzzy query Ajax Request data keyword is the keyword, pagenum is the corresponding page number
function Searchshopfn (KeyWord, pagenum) {
$.ajax ({
URL: $web _url + "XXXXR",//address of the request data,
DataType: "JSON",
Data: {
Keyword:keyword,
Brandid: ",
Province: ",
City: ",
Area: ",
Page:pagenum | | 1,
Size:10
},
Success:function (res) {
var liary = res.data.list;
var liarytotal = res.data.total;
Ishasnextpage = (Pagenum * < liarytotal)? True:false;
var m = ';
$.each (liary, function (i, v) {
M + = ' <li shopid= "' + v.id + '" title= "' + v.name + '" > ' + v.name + ' </li> ';
});
$ (' #searchResult '). Append (m);

}, Error:function (res) {

}

})
}


4. In this way, the corresponding fuzzy search can be implemented, following the input of an S, the results of the return show:




Front-end JS fuzzy search (fuzzy query)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.