Home Web Front-end JS Tutorial How to make intelligent keyword matching search with Ajax

How to make intelligent keyword matching search with Ajax

Apr 02, 2018 pm 05:42 PM
ajax Keywords intelligent

This time I will show you how to make intelligent keyword matching with AjaxSearch, what are the precautions for Ajax to make intelligent keyword matching search, the following is a practical case, Let’s take a look.

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); 
 $('.keywords_list').hide(); 
}
Copy after login

Rendering:

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to submit a form with ajax in Lavarel framework

Steps to implement Ajax loading progress bar Detailed explanation

The above is the detailed content of How to make intelligent keyword matching search with Ajax. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the 403 error encountered by jQuery AJAX request How to solve the 403 error encountered by jQuery AJAX request Feb 20, 2024 am 10:07 AM

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

How to solve jQuery AJAX request 403 error How to solve jQuery AJAX request 403 error Feb 19, 2024 pm 05:55 PM

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

PHP and Ajax: Building an autocomplete suggestion engine PHP and Ajax: Building an autocomplete suggestion engine Jun 02, 2024 pm 08:39 PM

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

How to solve the problem of jQuery AJAX error 403? How to solve the problem of jQuery AJAX error 403? Feb 23, 2024 pm 04:27 PM

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

In-depth analysis of the role and usage of the static keyword in C language In-depth analysis of the role and usage of the static keyword in C language Feb 20, 2024 pm 04:30 PM

In-depth analysis of the role and usage of the static keyword in C language. In C language, static is a very important keyword, which can be used in the definition of functions, variables and data types. Using the static keyword can change the link attributes, scope and life cycle of the object. Let’s analyze the role and usage of the static keyword in C language in detail. Static variables and functions: Variables defined using the static keyword inside a function are called static variables, which have a global life cycle

Huawei will launch the Xuanji sensing system in the field of smart wearables, which can assess the user's emotional state based on heart rate Huawei will launch the Xuanji sensing system in the field of smart wearables, which can assess the user's emotional state based on heart rate Aug 29, 2024 pm 03:30 PM

Recently, Huawei announced that it will launch a new smart wearable product equipped with Xuanji sensing system in September, which is expected to be Huawei's latest smart watch. This new product will integrate advanced emotional health monitoring functions. The Xuanji Perception System provides users with a comprehensive health assessment with its six characteristics - accuracy, comprehensiveness, speed, flexibility, openness and scalability. The system uses a super-sensing module and optimizes the multi-channel optical path architecture technology, which greatly improves the monitoring accuracy of basic indicators such as heart rate, blood oxygen and respiration rate. In addition, the Xuanji Sensing System has also expanded the research on emotional states based on heart rate data. It is not limited to physiological indicators, but can also evaluate the user's emotional state and stress level. It supports the monitoring of more than 60 sports health indicators, covering cardiovascular, respiratory, neurological, endocrine,

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

PHP vs. Ajax: Solutions for creating dynamically loaded content PHP vs. Ajax: Solutions for creating dynamically loaded content Jun 06, 2024 pm 01:12 PM

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.

See all articles