1. The issue of return value. Once return has a return value in the function, it will not execute the following statement and jump directly to the function call. As for the following PHP function code, if the first if condition meets, the function value returns Boolean false, which can return the value of a function and jump out of this function; as long as the return statement is encountered, the program will stop executing on that line of code, and the execution control will immediately return to the code that calls the program. function
function chkinput(form)
{
if(=="")
{
alert("Please enter the title of the article!");
();
return false;
}
if(=="")
{
alert("The article cannot be empty @!!");
();
return false;
}
return true;
}
The problem of triggering the onsubmit property, when will the onsubmit event be triggered? The onsubmit event occurs when the confirmation button in the form is clicked. The reasons for not triggering are generally as follows:
A. The triggering time of the onsubmit attribute will only be triggered when form is submitted with a button like input:submit, otherwise it will not be triggered. If you use a normal input:button, specify a javascript function in the onclick attribute, and execute the form submit() function in this function instead of the onsubmit attribute.
B. Let’s look at a piece of code first:
<form action="" method="post" onsubmit="submitTest();">
<INPUT value="www">
<input type="submit" value="submit">
</form>
<SCRIPT LANGUAGE="JavaScript">
<!--
function submitTest() {
// Some logical judgments return false;
}
//--></SCRIPT>
Click the submit button. The form is not submitted. Because there is one thing that should be changed to the following code:
<form action="" method="post" onsubmit="return submitTest();">It turns out that the onsubmit attribute is like a method name of the <form>html object, and its value (a string) is its method body, and the default return true;
Like Java, you can write as many statements in this method body, including built-in functions and custom functions.
Here, although submitTest() returns false, we only execute this function and do not process any results.
Onsubmit="return submitTest() uses its return value to achieve the expected effect. 3. The problem of event handling function returning false. In most cases, returning false for event handling function can prevent the default event behavior.
For example, by default, clicking on an <a> element will jump to the page specified by the href attribute of the element. Return False is equivalent to the terminator, and Return True is equivalent to the execution character. The effect of return false in js is generally used to cancel the default action. For example, if you click a link, in addition to triggering your onclick time (if you specify), you also have to trigger a default event, which is to execute the page jump. So if you want to cancel the default action of the object, you can return false. Return false is used more frequently in the occasions: <body> 1, <a href="/" mce_href="/" onclick='test();'>Hyperlink </a> 2, <input type="button" onclick='test()' value="submit"> 3, <form name="form1" onsubmIT="return test();"> Content <input type="submIT" value="submit"> </form> </body>
<input type="submit" onclick="submitAction(); return false;" /> The submitAction method contains the action to submit a form. If return false is not added,
After executing submitAction, the submit button will continue to execute its default event and the form will be submitted again. This may be the source of many mistakes. Indeed, the meaning of return false is not to prevent events from continuing to propagate to top-level elements, but to prevent browsers from processing events by default. You can experiment like this: first comment out all the js scripts, try to drag the image in the IE browser, and you will find that the mouse will become a prohibited style, and the image is prohibited from being dragged, which is the default behavior provided by the browser for mousemove event.
Return false is to remove this behavior, otherwise the interrupt event you described will be executed continuously. In addition, the equivalent statement to return false is:
= false,
You can replace return false with this statement and verify it. Finally, let me explain that this method is only applicable to IE browser.
The effect of return false in js is generally used to cancel the default action. For example, if you click a link, in addition to triggering your onclick time (if you specify), you also have to trigger a default event, which is to execute the page jump. So if you want to cancel the default action of the object, you can return false. Return false is used more frequently in occasions:
<form name="form1" onsubmit="return youfunction();">...... </form> <a href="www.***.com" onclick="...;return false;">dddd </a>
Copy the codeThe code is as follows:
function chkinput(form)
{
if(=="")
{
alert("Please enter the title of the article!");
();
return false;
}
if(=="")
{
alert("The article cannot be empty @!!");
();
return false;
}
return true;
}
The problem of triggering the onsubmit property, when will the onsubmit event be triggered? The onsubmit event occurs when the confirmation button in the form is clicked. The reasons for not triggering are generally as follows:
A. The triggering time of the onsubmit attribute will only be triggered when form is submitted with a button like input:submit, otherwise it will not be triggered. If you use a normal input:button, specify a javascript function in the onclick attribute, and execute the form submit() function in this function instead of the onsubmit attribute.
B. Let’s look at a piece of code first:
Copy the codeThe code is as follows:
<form action="" method="post" onsubmit="submitTest();">
<INPUT value="www">
<input type="submit" value="submit">
</form>
<SCRIPT LANGUAGE="JavaScript">
<!--
function submitTest() {
// Some logical judgments return false;
}
//--></SCRIPT>
Click the submit button. The form is not submitted. Because there is one thing that should be changed to the following code:
<form action="" method="post" onsubmit="return submitTest();">It turns out that the onsubmit attribute is like a method name of the <form>html object, and its value (a string) is its method body, and the default return true;
Like Java, you can write as many statements in this method body, including built-in functions and custom functions.
Here, although submitTest() returns false, we only execute this function and do not process any results.
Onsubmit="return submitTest() uses its return value to achieve the expected effect. 3. The problem of event handling function returning false. In most cases, returning false for event handling function can prevent the default event behavior.
For example, by default, clicking on an <a> element will jump to the page specified by the href attribute of the element. Return False is equivalent to the terminator, and Return True is equivalent to the execution character. The effect of return false in js is generally used to cancel the default action. For example, if you click a link, in addition to triggering your onclick time (if you specify), you also have to trigger a default event, which is to execute the page jump. So if you want to cancel the default action of the object, you can return false. Return false is used more frequently in the occasions: <body> 1, <a href="/" mce_href="/" onclick='test();'>Hyperlink </a> 2, <input type="button" onclick='test()' value="submit"> 3, <form name="form1" onsubmIT="return test();"> Content <input type="submIT" value="submit"> </form> </body>
<input type="submit" onclick="submitAction(); return false;" /> The submitAction method contains the action to submit a form. If return false is not added,
After executing submitAction, the submit button will continue to execute its default event and the form will be submitted again. This may be the source of many mistakes. Indeed, the meaning of return false is not to prevent events from continuing to propagate to top-level elements, but to prevent browsers from processing events by default. You can experiment like this: first comment out all the js scripts, try to drag the image in the IE browser, and you will find that the mouse will become a prohibited style, and the image is prohibited from being dragged, which is the default behavior provided by the browser for mousemove event.
Return false is to remove this behavior, otherwise the interrupt event you described will be executed continuously. In addition, the equivalent statement to return false is:
= false,
You can replace return false with this statement and verify it. Finally, let me explain that this method is only applicable to IE browser.
The effect of return false in js is generally used to cancel the default action. For example, if you click a link, in addition to triggering your onclick time (if you specify), you also have to trigger a default event, which is to execute the page jump. So if you want to cancel the default action of the object, you can return false. Return false is used more frequently in occasions:
<form name="form1" onsubmit="return youfunction();">...... </form> <a href="www.***.com" onclick="...;return false;">dddd </a>