Javascript Javascript返回false,仍然提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6412913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript return false, still submits form
提问by Bifterss
I have a form with JS validation, upon there being an error, the submit button should 'grey-out' and the form should not be submitted, however the last couple of functions seem to submit the form even though they pop the alert box!?!?!
我有一个带有 JS 验证的表单,出现错误时,提交按钮应该“变灰”并且不应该提交表单,但是最后几个函数似乎提交了表单,即使它们弹出了警告框! ?!?!
Button code:
按钮代码:
<input type="submit" name="button" id="button"
onclick='return formvalidation();' value="Next" />
Non Working Function Example:
非工作函数示例:
function BlankSite() {
var SiteNum= document.getElementsByName("sitesinput")[0].value;
if ((SiteNum == "") || (SiteNum == 0))
{
alert("You have not selected an amount of sites.")
document.forms[0].button.disabled=true;
return false;
}
}
Function initiator:
函数发起者:
function formvalidation()
{
ZeroPhones();
BlankPC();
BlankSite();
BlankSeats();
phone_change();
}// End of formvalidation
This is very strange and I have tried various work arounds all to no avail!
这很奇怪,我尝试了各种解决方法都无济于事!
Thanks, B.
谢谢,B。
采纳答案by Brad Christie
They are returning false (and breaking, which is actually unreachable code) but the results are never returned to parent validation function. Your validator, assuming it's bound to the form action, should look like:
他们返回 false (并且破坏,这实际上是无法访问的代码)但结果永远不会返回给父验证函数。您的验证器,假设它绑定到表单操作,应该如下所示:
function formvalidation(){
{
if (!ZeroPhones())
return false;
if (!BlankPC())
return false;
//
// keep checking for false and return false.
//
// default to return true
return true;
}
So when the functions do in-fact return false, the false return is carried back up through to the bound function.
因此,当函数实际上返回 false 时,false 返回值会返回到绑定函数。
回答by Shadow Wizard is Ear For You
You need to have return false;
in the function called by the onclick
, in this case formvalidation
.
在这种情况下,您需要return false;
在由 调用的函数中具有。onclick
formvalidation
Having some function called by the "root" function return false have no effect whatsoever, the return value is lost.
让“root”函数调用的某些函数返回 false 没有任何影响,返回值丢失。
回答by David Just
BlankPC() is called by formvalidation so false is returned into the method formvalidation(). your formvalidation() is always falling off the end which is the same as returning true. If you want it to return false when one of your validations fails it should be:
BlankPC() 由 formvalidation 调用,因此将 false 返回到方法 formvalidation() 中。您的 formvalidation() 总是从最后掉下来,这与返回 true 相同。如果您希望在其中一项验证失败时返回 false,则应该是:
function formvalidation()
{
retval = true;
retval &= ZeroPhones();
retval &= BlankPC();
retval &= BlankSite();
retval &= BlankSeats();
retval &= phone_change();
return retval;
}// End
This can be optimized a bunch, but you can get the gist of it.
这可以优化一堆,但你可以得到它的要点。
回答by Ravi Kant
call javascript function onSubmit of form instead of calling at button onClick.
调用表单的 javascript 函数 onSubmit 而不是在按钮 onClick 上调用。
javascript code
javascript代码
function validate()
{
alert('test');
return false;
}
<form action="test" method="post" onsubmit="return validate();">
this is working fine for me.
这对我来说很好用。
回答by Dave
formvalidation() isn't returning false.
formvalidation() 没有返回 false。
Maybe you want something like:
也许你想要这样的东西:
function formvalidation()
{
if(!ZeroPhones() ||
!BlankPC() ||
!BlankSite() ||
!BlankSeats() ||
!phone_change())
return false;
}
回答by Tommie C.
My solution to this problem was to disable the submit button until the validation was successful. Something along these lines.
我对这个问题的解决方案是禁用提交按钮,直到验证成功。沿着这些路线的东西。
function checkPassword() {
//this is my submit button
document.getElementById('nextBtn').setAttribute('disabled','disabled');
password1 = document.getElementsByName("pwd1")[0].value;
password2 = document.getElementsByName("pwd2")[0].value;
if (password1 == '') {
// If password not entered
alert ("Please enter Password");
return false;
} else if (password2 == ''){
// If confirm password not entered
alert ("Please enter confirm password");
return false;
} else if (password1 != password2) {
// If Not same return False.
alert ("\nPassword did not match: Please try again...");
return false;
} else {
document.getElementById('nextBtn').removeAttribute('disabled');
return true;
}
}