JavaScript 退出方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1859478/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:39:29  来源:igfitidea点击:

JavaScript exit method

javascript

提问by tommy

I have a JavaScript method that i want to validate a form

我有一个要验证表单的 JavaScript 方法

if the validation fails so

如果验证失败,那么

if (validationChecks ...... ){
    return alert("message")
}
else{
//proceed
}

however no matter what i do in the if the form still seems to submit, any ideas?

但是无论我做什么,如果表单似乎仍然提交,有什么想法吗?

回答by valli

you should return false after checking validation.

检查验证后,您应该返回 false。


if (validationChecks ...... ){ 
     alert("message") 
    return false; } 
else{ 
//procced 
} 

回答by Mark Bell

You have spelt 'return' wrong. Also, returning an alert will not work, you'll need to return false explicitly to stop the form submitting.

你拼错了'return'。此外,返回警报将不起作用,您需要明确返回 false 以停止提交表单。

if (validationChecks ...... ){ 
    alert("message");
    return false; 
} 
else{ 
//procced 
} 

回答by dxh

Your validation must return falsewhen the form is not valid, and if you're calling your validationin onSubmit, then you must make sure that you include a returnstatement there as well:

false当表单无效时,您的验证必须返回,如果您正在调用您的 validationin onSubmit,那么您必须确保在那里也包含一个return语句:

<form onSubmit="return validationChecks();">

回答by Tim Ebenezer

You need to return false;in the case of failure

你需要 return false;在失败的情况下