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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51578629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:36:40  来源:igfitidea点击:

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

javascripthtmlinputalert

提问by limusina10

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>

回答by baao

You can't put anything in an alert box. As the name indicates, it's an alert. You might be looking for a promptwhich has an input text field, or confirmto get a true / false depending on user selection.

您不能在警报框中放置任何东西。顾名思义,这是一个警报。您可能正在寻找具有输入文本字段的提示,或者根据用户选择确认获得真/假。

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

回答by Ioannis Papadopoulos

You can use

您可以使用

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

window.prompt is a blocking method (like alert). The program execution will halt until the user enters a value.

window.prompt 是一种阻塞方法(如警报)。程序执行将停止,直到用户输入一个值。

回答by Hello World

Just use var value = prompt('Your Question');

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

回答by Mojtaba Nava

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>