仅当用户在提示中单击取消 时,下面的代码才会运行alert('Please enter your name!');
。如果用户未在提示框中输入任何内容并单击 Enter,我试图让代码也运行所述警报。但是,当他们什么都不输入并单击 Enter 时,代码会呈现“Hello”。我该怎么做?
var theName = prompt('What is your name?');
if(theName != null) {
document.write('Hello, ' + theName);
}
else{
alert('Please enter your name!');
}
最佳答案
String. If the user clicks "OK", the input value is returned. If the user clicks "cancel", null is returned. If the user clicks OK without entering any text, an empty string is returned.;
关于JavaScript Prompt() 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45700404/