What is the function of return false in Jquey

Source: Internet
Author: User

What is the function of return false in Jquey:
In the numerous statements have the use of return false, of course, for the developers familiar with it, of course, know everything, know the role of this statement, of course, know when to use this statement, but for beginners may not be able to grasp the very clear, the following through an example to introduce return The function of the false statement.
the function of the return statement is generally to return the function value, and no longer execute the following statement, jump directly to the function call place, there is another important role, that is cancels the occurrence of the default event behavior .
The code example is as follows:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.softwhy.com/" /><title>Ant Tribe</title><Scripttype= "Text/javascript"src= "Mytest/jquery/jquery-1.8.3.js"></Script> <Scripttype= "Text/javascript">$ (document). Ready (function(){ $("a"). Click (function(){    return false; }) }) </Script> </Head> <Body> <DivID= "First"> <DivID= "Second">   <aID= "Third"href= "Http://www.softwhy.com">Link</a> </Div> </Div> </Body> </HTML>

From the above code can be seen, after clicking the link did not jump to the www.softwhy.com home, this is because return false to prevent the browser's default behavior, such as clicking on the hyperlink will be implemented Web page jump is the default behavior of the browser. Let's look at an example of a form validation below:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.softwhy.com/" /><title>Ant Tribe</title><Scripttype= "Text/javascript"src= "Mytest/jquery/jquery-1.8.3.js"></Script> <Scripttype= "Text/javascript">$ (document). Ready (function(){   $(": Submit"). Click (function(){     if($("#username"). Val ()=="") {alert ("the user name cannot be empty!"); $("#username"). focus (); return false; }     if($("#pw"). Val ()=="") {alert ("The password cannot be empty!"); $("#pw"). focus (); return false; }   }) }) </Script> </Head> <Body> <formAction= "Http://www.softwhy.com"name= "MyForm"> <ul>   <Li>User name:<inputtype= "text"ID= "username" /></Li>   <Li>Password:<inputtype= "Password"ID= "PW" /></Li>   <Li><inputtype= "Submit"value= "Submit Form"></Li> </ul> </form> </Body> </HTML>

In the above code, each judgment statement is added to the end of the return false statement, if the user name or password is empty, it will pop up a prompt box, if there is no return false statement, then even if you can pop up the prompt box, but the form will still be submitted, Because clicking the Submit form is the default event behavior of clicking the Submit button.

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=5827

For more information, refer to: http://www.softwhy.com/jquery/

What is the function of return false in Jquey

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.