简体   繁体   English

提交用于JavaScript验证的表单时,函数调用之前的返回结果是什么?

[英]When submitting a form for JavaScript validation what does the return before the function call do?

<form name="myForm" action="demo_form.asp" onsubmit="return validateForm();" method="post">

为什么validateForm()之前必须有return

If you don't use return , Javascript will call validateForm but will throw away the return value. 如果不使用return ,则Javascript会调用validateForm但会丢弃返回值。

If onsubmit returns false the form won't be submitted. 如果onsubmit返回false ,则不会提交表单。

The default submit action of the <form> can be stopped if the onsubmit function returns false . 如果onsubmit函数返回false则可以停止<form>的默认提交动作。

The value of the onsubmit attribute is treated like a function body, so the return is needed so that the form isn't submitted if it's not valid. onsubmit属性的值被视为函数体,因此需要return ,以便在表单无效时不提交表单。

Basically, what's happening in that inline handler is this: 基本上,该内联处理程序中发生的事情是这样的:

<button id="submit-button" type="submit" onclick="return validate()"></button>

var button = document.getElementById("submit-button");

button.onclick; // function () { return validate(); }

Versus: 与:

<button id="submit-button" type="submit" onclick="validate(event)"></button>

var button = document.getElementById("submit-button");

button.onclick; // equals function (event) { validate(event); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM