javascript:只有在检测到“返回假”时才执行 onsubmit

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

javascript: does onsubmit only execute when it detects a "return false"

javascripthtml

提问by user1666411

i dont quite understand how the onsubmit="return validate()"works. why do i have to returnthe function? does this work only when it detects a return falsefrom the statement?

我不太明白是如何onsubmit="return validate()"工作的。为什么我必须使用return该功能?仅当它return false从语句中检测到 a 时才有效吗?

<script type="text/javascript">
    function validate() {
        if ((document.example2.naming.value == "") || (document.example2.feed.value == "")) {
            alert("You must fill in all of the required .fields!");
            return false;
        }
        else {
            return true;
        }
</script>

<form name="example2" onsubmit="return validate()">
<input type="submit" name="B1" value="Submit">???????

回答by Ian

Using returnfor onsubmitdetermines whether the form actually submits or not (trueor false, respectively). This is useful for Javascript when you want to do something specific before allowing the form to submit or not. For example, like the code you provided, the point is to stop the form from submitting if the form isn't valid - if a certain field is blank. The important part is that you need to include returnin the onsubmit part, as well as actually returning trueor falsein the function you call. This is because the behavior for onsubmitis to always complete unless you return false. So when you do something like:

使用returnforonsubmit确定表单是否实际提交(分别为truefalse)。当您想在允许或不提交表单之前做一些特定的事情时,这对 Javascript 很有用。例如,就像您提供的代码一样,如果表单无效 - 如果某个字段为空,则停止表单提交。重要的部分是您需要包含return在 onsubmit 部分,以及实际返回truefalse您调用的函数中。这是因为onsubmit除非您返回,否则for 的行为将始终完成false。因此,当您执行以下操作时:

onsubmit="validate();"

nothing is returned (because of the lack of return) - so the function is run but that's it, so the form is submitted. When you add return, then the submitting depends on the return value - what's returned from the function. If you don't use return, it doesn't matter what the validatefunction returns, because the result of validateis not actually returned to onsubmit.

什么都没有返回(因为缺少return) - 所以函数被运行,但就是这样,所以表单被提交。当您添加时return,提交取决于返回值 - 从函数返回的内容。如果不使用return,则validate函数返回什么并不重要,因为 的结果validate实际上并未返回到onsubmit

回答by Jo?o Silva

When returning false, you preventthe formfrom being submitted. Otherwise, the form would be submitted, even though it may have validation errors, e.g., the feedvalue was empty.

return荷兰国际集团false,就防止form被提交。否则,表单将被提交,即使它可能有验证错误,例如,feed值为空。

On the other hand, if you return true, the form data will be submitted to the URL defined by the actionattribute.

另一方面,如果你return true,表单数据将被提交到action属性定义的 URL 。