


How to implement the automatic completion input box function in JavaScript?
How to implement the automatic completion input box function in JavaScript?
In web development, the auto-complete input box is a very common function. It can provide a fast, convenient and accurate input method and improve the user experience. This article will introduce how to use JavaScript to implement the auto-complete input box function and provide specific code examples.
1. HTML structure
First, we need to prepare an HTML structure containing an input box. An example is as follows:
<input type="text" id="autocomplete-input" placeholder="请输入关键字"> <ul id="autocomplete-list"></ul>
In the above example, we use the <input>
element as the input box, set an id to "autocomplete-input", and add a placeholder attribute. In addition, we use a <ul>
element as the auto-complete prompt list and set an id as "autocomplete-list".
2. JavaScript implementation
Next, we need to write JavaScript code to implement the automatic completion function. First, we need to listen to the input event of the input box and get the keywords entered by the user. We can then obtain the data related to the entered keyword through an Ajax request and display it in the auto-complete prompt list.
The following is a simple implementation example:
// 获取输入框和提示列表 var input = document.getElementById('autocomplete-input'); var list = document.getElementById('autocomplete-list'); // 监听输入框的输入事件 input.addEventListener('input', function() { var keyword = input.value; // 发送请求获取与关键字相关的数据 // 这里可以根据实际情况进行后端接口的调用 // 示例中使用一个静态的数据源来模拟请求 var data = ['apple', 'banana', 'orange', 'grape', 'pineapple']; // 清空提示列表 list.innerHTML = ''; // 遍历数据,在提示列表中插入匹配的项 for (var i = 0; i < data.length; i++) { if (data[i].indexOf(keyword) !== -1) { var item = document.createElement('li'); item.textContent = data[i]; list.appendChild(item); } } });
In the above example, we first obtain the DOM elements of the input box and prompt list, and use the addEventListener
method to listen for input The input event of the box. In the event handler function, we obtain the keyword entered by the user through the value
attribute.
We can then send an Ajax request to obtain data related to the keyword. In the example, in order to simplify the code, we use a static data source to simulate the request. The specific implementation needs to call the backend interface according to the actual situation.
Next, we insert items matching the keyword into the prompt list by looping through the data array. By creating a <li>
element, setting its text content using the textContent
attribute, and finally adding it to the prompt list.
3. Style beautification
Finally, we can beautify the auto-complete prompt list to improve the user experience. You can set the style of the prompt list through CSS, for example:
#autocomplete-list { border: 1px solid #ccc; list-style: none; padding: 0; max-height: 200px; overflow-y: auto; } #autocomplete-list li { padding: 5px; cursor: pointer; } #autocomplete-list li:hover { background-color: #f1f1f1; }
In the above example, we set the border style, list style, padding, maximum height and scroll bar of the prompt list.
Through the above steps, we can implement a simple auto-complete input box function. When the user enters a keyword, relevant data is automatically searched and matching items are displayed in the prompt list. Users can select and select an item through the mouse or keyboard to achieve automatic completion.
It should be noted that the above example is just a simple implementation, and the actual situation may require the cooperation of more complex processing logic and back-end interfaces. However, by understanding the above principles and examples, it can help us better implement and customize the auto-complete input box function.
The above is the detailed content of How to implement the automatic completion input box function in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
