如何使用JavaScript显示提示对话框?
当您要弹出文本框以获取用户输入时,提示对话框非常有用。因此,它使您可以与用户进行交互。用户需要填写该字段,然后单击“确定”。
使用称为的方法显示此对话框,该方法带有prompt()两个参数:(i)您要在文本框中显示的标签,以及(ii)要在文本框中显示的默认字符串。
该对话框有两个按钮:“确定”和“取消”。如果用户单击“确定”按钮,则窗口方法prompt()将从文本框中返回输入的值。如果用户单击“取消”按钮,则window方法prompt()将返回null。
示例
您可以尝试运行以下代码来获得如何使用JavaScript来实现提示对话框的方法:
<html>
<head>
<script>
<!--
function getValue(){
var retVal = prompt("Enter your name : ", "your name here");
document.write("You have entered : " + retVal);
}
//-->
</script>
</head>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type="button" value="Click Me" onclick="getValue();" />
</form>
</body>
</html>