Ajax implementation of simulated keyword intelligent matching search effect example tutorial

小云云
Release: 2023-03-19 12:04:01
Original
1743 people have browsed it

This article mainly introduces the Ajax implementation of simulated keyword intelligent matching search effect. When inputting content in the search box, a pop-up layer is displayed based on keyword matching. It is very good and has reference value. Friends who need it can refer to it. I hope Can help everyone.

Prepare data keyword.json: (only part of the data is posted here)


[ 
 {"id":1,"initial":"ad","keyword":"奥迪"}, 
 {"id":2,"initial":"ada4l","keyword":"奥迪A4L"}, 
 {"id":3,"initial":"ada6l","keyword":"奥迪A6L"}, 
 {"id":4,"initial":"adq5","keyword":"奥迪Q5"}, 
 {"id":5,"initial":"ada3","keyword":"奥迪A3"}, 
 {"id":6,"initial":"adq7","keyword":"奥迪Q7(进口)"}, 
 {"id":7,"initial":"ada8","keyword":"奥迪A8L(进口)"}, 
 {"id":8,"initial":"bm","keyword":"宝马"}, 
 {"id":9,"initial":"bm5x","keyword":"宝马5系"}, 
 {"id":10,"initial":"bm7x","keyword":"宝马7系"}, 
 {"id":11,"initial":"bt","keyword":"本田"}, 
 {"id":12,"initial":"bqsbx25","keyword":"北汽绅宝 X25"}, 
 {"id":13,"initial":"bqsbx35","keyword":"北汽绅宝X35"}, 
 {"id":14,"initial":"bqsbx55","keyword":"北汽绅宝X55"} 
]
Copy after login

html structure


<form class="fl search_form" action="#" method="post"> 
 <input class="search_text" id="searchKey" type="search" placeholder="请输入搜索关键字" onkeyup="searchSuggest(this);"/> 
 <input class="search_btn" type="submit" value="搜索"/> 
</form> 
<!--start--智能搜索关键字匹配弹出层--> 
<ul class="keywords_list"></ul> 
<!--end--智能搜索关键字匹配弹出层-->
Copy after login

js:


//当在搜索框输入内容时,根据关键字匹配,显示弹出层 
function searchSuggest(obj){ 
 var searchKey=$(obj).val(); 
 var reg = new RegExp(searchKey,"i"); //忽略大小写匹配搜索框中输入的内容 
 $.ajax({ 
 type:"get", 
 url:"data/keyword.json", 
 dataType:"json", 
 success:function(data){ 
  var arr=[]; 
  for(var i=0,len=data.length;i<len;i++){ 
  if(searchKey!="" && (data[i].initial.search(reg)!=-1 || data[i].keyword.search(reg)!=-1)) { 
   arr.push("<li onclick=&#39;changeSearchKey(this);&#39;>"+data[i].keyword+"</li>"); 
  } 
  }     
  $(".keywords_list").html(arr).show(); 
 } 
 }); 
} 
//单击匹配列表中的关键字选项时,将该关键字显示在搜索框中 
function changeSearchKey(obj){ 
 var value=$(obj).text(); 
 $("#searchKey").val(value); 
 $(&#39;.keywords_list&#39;).hide(); 
}
Copy after login

Rendering:

##Related recommendations:


JavaScript's replace implementation of search keyword highlighting method

Detailed explanation of the super keyword in ES6 Class

jq, ajax, php, mysql implement keyword fuzzy query

The above is the detailed content of Ajax implementation of simulated keyword intelligent matching search effect example tutorial. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template