[英]Double Alert Windows
I am getting double Alert prompts for a single form: The alert pops up the first time, then when I click OK, the same one pops open again. 我同时收到一个窗体的警报提示:第一次弹出警报,然后单击“确定”,再次弹出相同的警报。 I think my code should do the alert once when it hits "else window.alert(sameWarning) and the stop when it reaches "return false()"; but it seems return false() is causing the alert twice. Maybe because I have two forms on one page; yet, they have two separate function names ("signinForm()" and "saveSignUp()")and call two separate forms (forms[0] and forms[1])? I think my code is correct, but I can't figure it out at this point. Any help would be appreciated. Thanks in advance! 我认为我的代码在遇到“ else window.alert(sameWarning)”时应该执行一次警报,并在到达“ return false()”时停止;但是似乎return false()导致了两次警报。也许是因为一页上有两个表单;但是,它们有两个单独的函数名(“ signinForm()”和“ saveSignUp()”)并调用两个单独的表单(forms [0]和forms [1])吗?我认为我的代码是正确的,但目前无法解决,请提供任何帮助,在此先感谢!
<script type="text/javascript">
//FIRST FUNCTION FOR FORMS[0]
function signinForm(){
// var sameWarning = "The required fields were not submitted for the following error(s). \n \n";
var warningLogin = "The following field(s) require an entry: \n \n";
var sameWarning = warningLogin;
var userName = document.forms[0].userName.value;
var pswd = document.forms[0].pswd.value;
if(userName == ""){
sameWarning += " - First Name \n";
}
if(pswd == ""){
sameWarning += " - Password \n";
}
if (warningLogin == sameWarning){
return true;
}
else {
window.alert(sameWarning);
}
return false;
}
//SECOND FUNCTION CALL FOR FORMS[1]
function saveSignUp(){
// var warning = "The required fields were not submitted for the following error(s). \n \n";
var warning = "The following field(s) require an entry: \n \n";
var same = warning;
var firstName = document.forms[1].firstName.value;
var lastName = document.forms[1].lastName.value;
var email = document.forms[1].email.value;
var phone = document.forms[1].phoneNumber.value;
if(firstName == ""){
//firstName.style.backgroundColor="red";
warning += " - First Name \n";
}
if(lastName == ""){
warning += " - Last Name \n";
}
if(email == ""){
warning += " - Email \n";
}
if(phone == ""){
warning += " - Work Phone \n";
}
if(phone < 5 ){
warning += " - Must be a numberssss \n";
}
if (warning == same){
return true;
}
else {
alert(warning);
}
return false;
}
</script>
HTML 的HTML
<h1>Login</h1>
<form method="post" onsubmit="return signinForm();" action="" >
<input type="text" placeholder="Username/Email" name="userName">
<input type="password" placeholder="Password" name="pswd" id="pswd">
<input type="submit" onclick="signinForm();" value="Sign In">
</form>
<h1>Sign Up!</h1>
<form method="post" onsubmit="return saveSignUp()" action="" >
<input type="text" placeholder="First Name" name="firstName">
<input type="text" placeholder="Last Name" name="lastName">
<input type="text" placeholder="Email" name="email">
<input type="text" placeholder="Phone Number" name="phoneNumber">
<input type="submit" onclick="saveSignUp();" class="button wide" value="Request an Account">
It is because you call signinForm();
这是因为您调用了signinForm();
twice. 两次。
Remove one of these listeners 删除这些监听器之一
onclick="signinForm();"
onsubmit="return signinForm()"
When you click on submit button you call signinForm();
当您单击提交按钮时,您调用signinForm();
. 。 And because it is a submit
button, it will trigger the onsubmit
event, and call return signinForm()
并且由于它是一个submit
按钮,它将触发onsubmit
事件,并调用return signinForm()
When you click submit, first it fires the click event (calling your function), then it fires a submit event (calling your function again). 当您单击提交时,首先触发click事件(调用您的函数),然后触发一个Submit事件(再次调用您的函数)。 Just remove the onclick for the submit buttons. 只需删除提交按钮的onclick即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.