我目前正在研究w3schools的一些JavaScript示例,我想知道是否有办法获取用户输入的文本信息,同时确定用户是单击“确定”还是取消。
我知道确定是否按下OK或取消:
var r=confirm("Press a button");
if (r==true)
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
}
从文本字段中获取数据:
var name=prompt("Please enter your name","Harry Potter");
if (name!=null && name!="")
{
document.write("<p>Hello " + name + "! How are you today?</p>");
}
但有没有办法同时获得这两个?
由于
答案 0 :(得分:2)
var name = prompt("Please enter your name", "Harry Potter");
if (name == null)
alert("CANCEL");
else if (name == "")
alert("OK BUT BLANK");
else
alert("YOU ENTERED "+ name);
答案 1 :(得分:2)
var p;
p = prompt('What is your age ?','');
if (p == null) {
alert('Cancel was clicked.');
}
else {
alert('OK was clicked.');
}
如果用户点击取消或按X,则p
为空