Home  >  Article  >  Web Front-end  >  Using JavaScript to implement form auto-completion

Using JavaScript to implement form auto-completion

王林
王林Original
2023-08-08 08:36:161491browse

Using JavaScript to implement form auto-completion

Use JavaScript to implement form auto-completion function

With the development of the Internet, web forms play a very important role in our daily lives. In order to improve the user experience and provide users with more convenience, the form auto-completion function has become an indispensable feature. This article will introduce how to use JavaScript to implement the form auto-completion function and provide corresponding code examples.

First, we need a database or data source that contains auto-completion options. These options can be prepared in advance by ourselves or obtained from the server. In this article, we will use a simple array as the data source, the example is as follows:

var autofillOptions = ["apple", "banana", "cherry", "durian", "elderberry", "fig", "grape", "honeydew", "imbe", "jackfruit"];

Next, we need to add event listeners for the form elements entered by the user. When the user starts typing, we will display the autocomplete option. The code example is as follows:

var inputElement = document.getElementById("input"); // 获取表单元素

inputElement.addEventListener("input", function() {
  var inputText = inputElement.value; // 获取用户输入的文本
  var autocompleteList = document.getElementById("autocomplete-list"); // 获取自动补全选项列表元素

  // 清空自动补全选项列表
  autocompleteList.innerHTML = "";

  // 遍历数据源,匹配用户输入的文本
  autofillOptions.forEach(function(option) {
    if (option.startsWith(inputText)) {
      // 创建一个自动补全选项项
      var optionElement = document.createElement("div");
      optionElement.textContent = option;

      // 添加点击事件监听器,将选项填入表单元素
      optionElement.addEventListener("click", function() {
        inputElement.value = option;
        autocompleteList.innerHTML = "";
      });

      // 将选项添加到自动补全选项列表中
      autocompleteList.appendChild(optionElement);
    }
  });
});

In the above code, we first obtain the form element entered by the user and add an input event listener to the element. Each time the user inputs, the event listener will be triggered and dynamically generate auto-complete options based on the text entered by the user.

Next, we iterate through each option in the data source and use the startsWith() method to determine whether the option starts with the text entered by the user. If so, we create a div element containing the options text and add a click event listener to that element. This listener will populate the form element with options and clear the autocomplete option list when the user clicks on the option.

Finally, we add the generated autocomplete options to an element called autocomplete-list. You can define this element as a hidden drop-down list, or use styles to hide it.

To summarize, using JavaScript to implement the form auto-completion function can improve the user experience and allow users to enter form content faster. We can easily implement this feature by listening to the input events of the form elements, dynamically generating auto-complete options, and filling in the form elements when the user clicks on the options. I hope the code examples provided in this article will be helpful to you.

The above is the detailed content of Using JavaScript to implement form auto-completion. 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