我正在尝试使用 javascript 验证 html 表单中的 3 个字段。如果任何字段为空,则会出现一个警告框,并显示一条消息,指示哪些字段为空。这一切都很好。当表单正确填写后,我无法在警报框中显示消息 (msg2)。我的代码如下 - 我知道这只是一些简单的事情,如果有人可以提供帮助的话,我会错过。谢谢!
var valid = true;
var msg="Incomplete form:\n";
var msg2="Success! There are no null fields.";
if ( myname== "" ) {
msg+="You need to fill the name field!\n";
valid = false;
}
if ( emailaddress == "" ) {
msg+="You need to fill in your email!\n";
valid = false;
}
if ( commentString == "" ) {
msg+="You need to fill in your comment!\n";
valid = false;
}
if ((!myname=="")&&(!emailaddress=="")&&(!commentString=="")){
return msg2;
}
if (!valid) alert(msg);
return valid;
}
最佳答案
你是对的,这很简单:return msg2;
不会打开警报框。您仍然需要在某个地方调用 alert()
。
关于javascript - 为什么 JavaScript 警报框在此示例中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23061098/