How to use JavaScript to realize the automatic prompt function of the input box content of the form?
Introduction: The automatic prompt function of the form input box content is very common in web applications. It can help users quickly enter the correct content. This article explains how to implement this functionality using JavaScript and provides specific code examples.
First, we need to create an HTML structure that contains an input box and an auto-suggestion list. You can use the following code:
表单输入框内容自动提示
In this example, we create an input box and use theonkeyup
event to call theshowHint()
function while on the page Create a paragraph tag to display auto-suggested content.
Next, we need to implement the automatic prompt function in JavaScript. You can use the following code:
function showHint() { var input = document.getElementById("myInput").value; var hint = document.getElementById("hint"); // 在这里进行数据查询,获取提示列表 var hints = getHints(input); if (hints.length > 0) { hint.innerHTML = hints.join(", "); // 将提示内容拼接为字符串,并显示在页面上 } else { hint.innerHTML = ""; // 如果没有提示内容,则清空提示 } } function getHints(input) { // 这里可以根据实际需求,从服务器或本地数据获取提示列表 // 并根据输入的内容进行筛选和匹配 // 假设这里是一个静态的提示列表 var hints = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape"]; // 根据输入的内容进行筛选和匹配 hints = hints.filter(function(item) { return item.toLowerCase().indexOf(input.toLowerCase()) > -1; }); return hints; }
In this example, theshowHint()
function will be triggered when the content of the input box changes. It will get the value of the input box and call thegetHints()
function to get the list of hints. Then, decide whether to display the prompt content based on the length of the list.
getHints()
The function is a simple example that assumes a static list of hints and filters and matches based on the input content. In practical applications, the prompt list can be obtained from the server or local data according to needs, and more complex algorithms can be used for matching.
In order to make the page more beautiful, we can add some styles to the input box and prompt list. You can use the following code:
In this example, we set the padding and font size of the input box, as well as the top margin, font size and color of the prompt list.
Summary:
Through the above steps, we successfully used JavaScript to implement the automatic prompt function for the input box content of the form. By listening to the input change event of the input box, we obtain the content entered by the user, filter and match it from the server or local data based on the content, and finally display the matching results on the page.
This function can help users quickly enter correct content and improve user experience. It can also be used in various web applications, such as the association function of the search box, automatic completion of input addresses, etc.
I hope the examples in this article can help readers understand how to use JavaScript to realize the automatic prompt function of the input box content of the form. Readers can make corresponding modifications and extensions according to their own needs to meet the requirements of practical applications.
The above is the detailed content of How to use JavaScript to realize the automatic prompt function of the input box content of the form?. For more information, please follow other related articles on the PHP Chinese website!