Home > Web Front-end > JS Tutorial > How to enter data in javascript

How to enter data in javascript

青灯夜游
Release: 2023-01-07 11:43:52
Original
17750 people have browsed it

How to input data in javascript: 1. Use the prompt() method, which will pop up a dialog box that allows input values. It provides two buttons, OK and Cancel, and also provides the expected input value; 2. Use comfirm () method, pops up a confirmation message dialog box, and can get the true or false value.

How to enter data in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript input statement

JS provides two input methods prompt() and confirm(), let’s learn about them next;

1. prompt() method

Pops up a dialog box that allows you to enter a value, with two buttons to confirm and cancel, and allows you to set the default value

Syntax format:

prompt(msg,defaultText)
Copy after login
  • msg: the text information to be displayed;

  • defaultText: the default value of the input box;

Example:

 <script>
       function inBtn(){
           var msg = prompt("请输入值","预期输入");
           alert("你输入的值:" + msg);
       }
    </script>
    <button οnclick="inBtn()">打开输入框</button>
Copy after login

How to enter data in javascript

2. confirm() method

pops up a confirmation message dialog box with two buttons: OK and Cancel. Click OK to return true, and click Cancel to return false.

A dialog box pops up, showing the edited content, with two buttons: OK and Cancel. Click OK to return true, click Cancel to return false;

Syntax format:

confirm(message);  //message为想要显示的内容
Copy after login

Example:

<body>
    <script>
       function inBtn(){
           var msg = confirm("你学过Javascript吗?");
           if(msg){
               alert("学过了,那还需要努力!");
               var msg2 = confirm("我将提供一个教程给你,需要吗?");
               if(msg2) 
                    alert("w3cschool");
               else 
                    alert("那可真是太遗憾了");
           }else{
               alert("没学过,那就开始学吧!");
           }
       }
    </script>
    <button οnclick="inBtn()">打开输入框</button>
</body>
Copy after login

The animation demonstration effect is as follows:

How to enter data in javascript

##[Recommended learning:

javascript advanced tutorial]

The above is the detailed content of How to enter data in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template