Issues with OnClick (), submit () and onsubmit () in form forms
Recently encountered a filter processing the form data, using the button's OnClick event to check, found that return false after the form is still committed.
The relationship and difference between the set functions of the onclick, onsubmit and submit are studied carefully.
12345678910111213 |
onsubmit:
You can override this event by returning false in the event handler.
Use this capability to validate data on the client side to prevent invalid data from being submitted to the server.
If the event handler is called by the onsubmit attribute of the form object,
the code must explicitly request the return value using the return function,
and the event handler must provide an explicit return value for each possible code path in the event handler function.
The submit method does not invoke the onsubmit event handler.
submit:
The submit method does not invoke the onsubmit event handler.
Call the onsubmit event handler directly.
When using Microsoft? Internet Explorer 5.5 and later,
you can call the fireEvent method with a value of onsubmit in the sEvent parameter.
|
First create a form
1234 |
<
form action
=
"#" method
=
"POST" name
=
"A" onsubmit
=
"return X();"
>
<
input type
=
"text" value
=
"" />
<
input onclick
=
"Y()" type
=
"submit" value
=
"提交" />
</
form
>
|
Write your own x (), Y () function, and we'll find out the order in which these functions are executed
1) onclick:y ();
2) onsubmit:x ();
3) submit ();
Other words
As long as the onclick does not return false then continue execution onsubmit
As long as onsubmit does not return false then the form is submitted.
Another point of note must be "return X ();" To get the return value of the function, otherwise just call the function, the return value is not passed
Correct wording:
<input type=submit onclick= "return X ();" >
X () returns false, the form's submit will be terminated
Error wording:
<input type=submit onclick= "X ()" >
X () returns false after not passed to the OnClick event, the form's submit will continue
Form form onclick () and onsubmit ()