简体   繁体   English

如何在 alert() 框中输入输入?

[英]How can I put an input in the alert() box?

I have a question that I want to put an input in an alert box.我有一个问题,我想在警告框中输入一个输入。 What thing I have to do to create this?我必须做什么才能创建这个? To make it I've to put an another tag, attrib, special properities, etc... Thanks.为此,我必须添加另一个标签、属性、特殊属性等……谢谢。 I think could be like this:我想可能是这样的:

 <!DOCTYPE html> <html> <head> <title>example</title> </head> <body> <script type="text/javascript"> alert("<input></input>"); </script> </body> </html>

You can't put anything in an alert box.您不能在警报框中放置任何东西。 As the name indicates, it's an alert.顾名思义,这是一个警报。 You might be looking for a prompt which has an input text field, orconfirm to get a true / false depending on user selection.您可能正在寻找具有输入文本字段的 提示,或者根据用户选择确认获得真/假。

 let foo = prompt('Type here'); let bar = confirm('Confirm or deny'); console.log(foo, bar);

You can use您可以使用

var resp = window.prompt("Your question")

window.prompt is a blocking method (like alert). window.prompt 是一种阻塞方法(如警报)。 The program execution will halt until the user enters a value.程序执行将停止,直到用户输入一个值。

只需使用var value = prompt('Your Question');

This code been executed:这段代码被执行:

 <!DOCTYPE html> <html> <body> <h2>JavaScript Confirm Box</h2> <button onclick="myFunction()">Try it</button> <input id="demo" /> <script> function myFunction() { var txt; if (confirm("Press a button!")) { txt = "You pressed OK!"; } else { txt = "You pressed Cancel!"; } document.getElementById("demo").innerHTML = txt; console.log(txt); } </script> </body> </html>

window object comes with three methods for showing user dialog boxes window对象自带三种显示用户对话框的方法

this methods will halt current executing program till user provide any response to this box此方法将停止当前正在执行的程序,直到用户对此框提供任何响应

window.prompt(text, defaultText)

this will take value from user for further process这将从用户那里获取价值以进行进一步的处理

window.alert(message)

this will only alert user for his action.这只会提醒用户他的行为。

window.confirm(message)

this will take user response in boolean form for further process这将采用布尔形式的用户响应进行进一步处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM