Return false: In most cases, false is returned for the event handler to prevent default event behavior. As we all know, adding onsubmit = "Return false;" to a form can prevent form submission.
Below is a simple Section
Code :
| Code: |
<Form action = "index. jsp" method = "Post" onsubmit = "submittest ();">
<Input value = "www">
<Input type = "Submit" value = "Submit">
</Form>
<Script language = "JavaScript">
<! --
Function submittest (){
// Some logical judgments
Return false;
}
// -->
</SCRIPT> |
You can determine whether the form is submitted by clicking the submit button as described above?
If the answer is yes, you don't have to look down.
If the answer is no, it is incorrect. The actual situation is that the form is submitted normally. If you do not want it to be submitted, you should
| Code: |
| <Form action = "index. jsp" method = "Post" onsubmit = "submittest ();"> |
Change
| Code: |
| <Form action = "index. jsp" method = "Post" onsubmit = "Return submittest ();"> |
Why?
The onsubmit attribute is like a method name of the <form> HTML object. Its value (a string) is its method body. By default, true is returned;
Like java, you can write any number of statements in the method body, including built-in functions and custom functions, such
| Code: |
Onsubmit ="
Alert ('hahaha'); // built-in functions
Submittest (); // User-Defined Functions
Alert (this. tagname); // This keyword is used.
... (Any number of statements)
Return false;
" |
It is equivalent
| Code: |
Form. Prototype. onsubmit = function (){
Alert ('hahaha'); // built-in functions
Submittest (); // User-Defined Functions
Alert (this. tagname); // This keyword is used.
... (Any number of statements)
Return false;
}; |
In this case, you overwrite (override) the default method (true is returned by default)
You have noticed that the keyword "this" can be used in the method body, which represents the object instance of <form>.
After such analysis, it is difficult to understand the above situation:
| Code: |
| <Form action = "index. jsp" method = "Post" onsubmit = "submittest ();"> |
Write the override method as follows:
| Code: |
Form. Prototype. onsubmit = function (){
Submittest ();
}; |
Although submittest () returns false, we only execute this function and do not process the result. While
| Code: |
| <Form action = "index. jsp" method = "Post" onsubmit = "Return submittest ();"> |
The effects of the override method are as follows:
| Code: |
Form. Prototype. onsubmit = function (){
Return submittest ();
}; |
In this way, we use its return value to achieve the expected results.
In this way, I think it will be much more impressive and it will not be easy to make mistakes.
Conclusion:
We can use the way of thinking in Java to simulate similar situations in JavaScript (prototype-Based Object-Oriented Technology in JavaScript does ), however, after all, they are essentially different. For example, Java is strongly typed and has strict syntax restrictions, while Javascript is loose. As shown in the preceding method:
| Code: |
Form. Prototype. onsubmit = function (){
}; |
both return values and no return values are allowed, which is not acceptable in Java, after all, the methods in Java cannot be overloaded by different return values, while the overloading in Javascript is much loose.