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

How to enter data in javascript

青灯夜游
青灯夜游Original
2021-07-16 15:54:2417633browse

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)
  • 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>

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为想要显示的内容

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>

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!

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