Home >Web Front-end >JS Tutorial >Usage of prompt() in js
prompt() method is used to open a pop-up window to prompt the user to enter text and return the user input (or null). Usage: 1. Pass the prompt text parameter. 2. Provide optional default value parameters. Returns null when the user clicks to cancel or close the window. The default input type is text, and you can create a numeric input field by specifying "number" as the second parameter.
prompt() method introduction
prompt() method is used to open a pop-up window in a web page to prompt User enters text. This method returns the text entered by the user, or null if the user clicks the Cancel button or closes the window.
Usage
To use the prompt() method, please pass two parameters:
<code class="js">let userInput = prompt("请输入您的姓名:", "Jane Doe");</code>
Example
The following example shows how to use the prompt() method to collect user input:
<code class="html"><script> function getInput() { let name = prompt("请输入您的姓名:"); alert("您输入的姓名是:" + name); } </script> <button onclick="getInput()">获取用户输入</button></code>
When the user clicks button, a popup will open prompting the user to enter their name. After the user clicks OK, the entered text is stored in the name variable and then displayed in the browser using the alert() method.
Tip
The above is the detailed content of Usage of prompt() in js. For more information, please follow other related articles on the PHP Chinese website!