search
HomeWeb Front-endJS TutorialHow to use JavaScript to realize the automatic completion prompt function of the input box content of the form?

如何使用 JavaScript 实现表单的输入框内容自动补全提示功能?

How to use JavaScript to implement the automatic completion prompt function of the input box content of the form?

With the development of the Internet, the automatic completion prompt function of form input boxes is becoming more and more common. When users enter content, possible completion options will be provided based on existing data to facilitate users to quickly select or enter the correct information. This article will introduce how to use JavaScript to implement the automatic completion prompt function of the input box content of the form, and provide specific code examples.

Step One: Create HTML Structure

First, we need to create a simple HTML structure and add a text input box to it. An example is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>表单自动补全提示</title>
</head>
<body>
    <form>
        <input type="text" id="inputBox" autocomplete="off">
    </form>

    <ul id="suggestionList"></ul>
</body>
</html>

In the above code, we create a text input box with the id "inputBox" and disable the default auto-complete function (autocomplete="off" ). In addition, we also created an empty unordered list ul to display the auto-completion prompt options.

Step 2: Implement the JavaScript function

Next, we use JavaScript to implement the form’s auto-complete prompt function. In the code, we will listen to the keyboard events of the input box. When the user enters content, the auto-complete prompt options will be generated and displayed based on the existing data.

// JavaScript 代码
var inputBox = document.getElementById('inputBox'); // 获取输入框元素
var suggestionList = document.getElementById('suggestionList'); // 获取提示选项的列表元素

// 模拟已存在的数据
var data = ['apple', 'banana', 'cherry', 'grape', 'orange'];

inputBox.addEventListener('input', function() {
    var inputValue = inputBox.value; // 获取输入框的值
    suggestionList.innerHTML = ''; // 清空提示选项列表

    // 根据输入内容生成提示选项
    var suggestions = data.filter(function(item) {
        return item.includes(inputValue);
    });

    suggestions.forEach(function(item) {
        var li = document.createElement('li');
        li.textContent = item;
        suggestionList.appendChild(li);
    });
});

// 点击提示选项时,将选项的值填入输入框
suggestionList.addEventListener('click', function(e) {
    var selectedValue = e.target.textContent;
    inputBox.value = selectedValue;
});

In the above code, we first obtain the input box element and the list element of the prompt option. Then, we simulated an existing set of data. Next, we listen to the input event of the input box and trigger the handler function when the user enters content.

The processing function first obtains the value of the input box, and then clears the prompt option list. Afterwards, the prompt options that meet the conditions are filtered out from the existing data based on the input content. During the filtering process, we used the includes method to determine whether the existing data contains the input content. Finally, we add the generated prompt options to the prompt options list one by one.

In addition, we also listened to the click event of the prompt option list. When the user clicks on a prompt option, the value of the option is filled in the input box.

Summary:

It is very simple to use JavaScript to implement the automatic completion prompt function of the input box content of the form. We only need to listen to the input event of the input box, generate and display prompt options based on existing data, and then fill in the value of the input box when the user selects an option. In this way, users can enter correct information more conveniently, improving the user experience. I hope the introduction in this article can help everyone.

The above is the detailed content of How to use JavaScript to realize the automatic completion prompt function of the input box content of the form?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
如何实现PHP表单提交后的页面跳转如何实现PHP表单提交后的页面跳转Aug 12, 2023 am 11:30 AM

如何实现PHP表单提交后的页面跳转【简介】在Web开发中,表单的提交是一项常见的功能需求。当用户填写完表单并点击提交按钮后,通常需要将表单数据发送至服务器进行处理,并在处理完后将用户重定向至另一个页面。本文将介绍如何使用PHP来实现表单提交后的页面跳转。【步骤一:HTML表单】首先,我们需要在HTML页面中编写一个包含表单的页面,以便用户填写需要提交的数据。

如何处理PHP表单中的用户权限管理如何处理PHP表单中的用户权限管理Aug 10, 2023 pm 01:06 PM

如何处理PHP表单中的用户权限管理随着Web应用程序的不断发展,用户权限管理是一个重要的功能之一。用户权限管理可以控制用户在应用程序中的操作权限,保证数据的安全性和合法性。在PHP表单中,用户权限管理可以通过一些简单的代码来实现。本文将介绍如何处理PHP表单中的用户权限管理,并给出相应的代码示例。一、用户角色的定义和管理首先,对用户角色进行定义和管理是用户权

PHP表单处理:表单数据查询与筛选PHP表单处理:表单数据查询与筛选Aug 07, 2023 pm 06:17 PM

PHP表单处理:表单数据查询与筛选引言在Web开发中,表单是一种重要的交互方式,用户可以通过表单向服务器提交数据并进行进一步的处理。本文将介绍如何使用PHP处理表单数据的查询与筛选功能。表单的设计与提交首先,我们需要设计一个包含查询与筛选功能的表单。常见的表单元素包括输入框、下拉列表、单选框、复选框等,根据具体需求进行设计。用户在提交表单时,会将数据以POS

Java实现表单的实时验证与提示功能Java实现表单的实时验证与提示功能Aug 07, 2023 am 10:42 AM

Java实现表单的实时验证与提示功能随着网络应用的普及和发展,表单的使用也变得越来越重要。表单是网页中用于收集和提交用户数据的元素,例如注册或登录页面的表单。在用户填写表单时,经常需要对其输入的数据进行验证和提示,以保证数据的正确性和完整性。在本文中,我们将介绍如何使用Java语言实现表单的实时验证与提示功能。HTML表单的搭建首先,我们需要使用HTML语言

如何使用PHP处理表单中的数据搜索和过滤如何使用PHP处理表单中的数据搜索和过滤Aug 12, 2023 pm 04:00 PM

如何使用PHP处理表单中的数据搜索和过滤概要:当用户通过表单提交数据时,我们需要对这些数据进行搜索和过滤,以便得到所需的结果。在PHP中,我们可以使用一些技术来实现这些功能。本篇文章将介绍如何使用PHP处理表单中的数据搜索和过滤,并提供相应的代码示例。简介:表单通常用于收集用户的输入数据,这些数据可以是文本、数字、日期等等。一旦用户提交表单,我们就需要对这些

如何在Nette框架中使用表单和验证?如何在Nette框架中使用表单和验证?Jun 04, 2023 pm 03:51 PM

Nette框架是一款用于PHPWeb开发的轻量级框架,以其简单易用、高效稳定的特点受到了广泛的欢迎和使用。在开发Web应用时,使用表单和验证是不可避免的需求。本文将介绍如何在Nette框架中使用表单和验证。一、表单构建在Nette框架中,表单可以通过Form类来创建。Form类在NetteForms命名空间中,可以通过use关键字引入。useNetteF

如何处理PHP表单中的下拉列表选项如何处理PHP表单中的下拉列表选项Aug 11, 2023 am 10:21 AM

如何处理PHP表单中的下拉列表选项下拉列表是Web表单中常用的元素,它允许用户从预先定义的选项中选择一个或多个值。在PHP中,我们可以通过一些简单的代码实现下拉列表的处理。本文将向你展示如何使用PHP来处理表单中的下拉列表选项。HTML代码中的下拉列表通常使用&lt;select&gt;和&lt;option&gt;标签来定义。&lt;select&gt;标

ThinkPHP6表单重复提交处理:防止重复操作ThinkPHP6表单重复提交处理:防止重复操作Aug 12, 2023 pm 02:10 PM

ThinkPHP6表单重复提交处理:防止重复操作在Web应用程序开发中,表单提交是一项常见的操作。但是,有时用户会因为网络延迟或者误操作造成表单的重复提交,这样会给系统带来一些问题。为了解决这个问题,我们可以在ThinkPHP6框架中进行表单重复提交处理,以防止用户重复操作。一、原因分析造成表单重复提交的原因主要有两个:1.网络延迟:当用户点击提交按钮后,表

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor