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.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JS provides two input methods prompt() and confirm(), let’s learn about them next;
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>
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:
##[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!