<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.
If onsubmit
returns false
the form won't be submitted.
The default submit action of the <form>
can be stopped if the onsubmit
function returns false
.
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.
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); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.