Home >Web Front-end >JS Tutorial >Usage of prompt() in js

Usage of prompt() in js

下次还敢
下次还敢Original
2024-05-01 06:21:141127browse

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.

Usage of prompt() in js

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:

  • The first parameter: to be displayed to the user prompt text.
  • Second parameter (optional): Provide a default value for the input field.
<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

  • This method will return null if the user clicks the Cancel button or closes the window.
  • By default, the input type is text. A numeric input field can be created by specifying "number" in the second parameter.
  • The prompt() method is synchronous, which means it blocks execution until the user closes the window.

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!

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
Previous article:How to use include in jsNext article:How to use include in js