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 return statement generally functions as a return function value, and no longer executes the following statement, jumping directly to where the function is called, and another important function is to cancel the occurrence of the default event behavior.
The code example is as follows:

?
1234567891011121314151617181920212223 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>脚本之家</title> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script<script type="text/javascript"$(document).ready(function(){  $("a").click(function(){    return false;  })  })  </script</head<body<div id="first"<div id="second" <a id="third" href=<A href="http://www.jb51.net">http://www.jb51.net</A>>链接</a</div</div</body</html>

From the above code can be seen, after clicking the link did not jump to the http://www.jb51.net 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:

?
123456789101112131415161718192021222324252627282930313233343536 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.jb51.net/" /> <title>脚本之家</title> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script<script type="text/javascript"$(document).ready(function(){   $(":submit").click(function(){    if($("#username").val()=="")       alert("用户名不能为空!");     $("#username").focus();     return false;      if($("#pw").val()=="")       alert("密码不能为空!");     $("#pw").focus();     return false;     })  })  </script</head<body<form action="http://www.jb51.net" name="myform"<ul <li>用户名:<input type="text" id="username" /></li <li>密码:<input type="password" id="pw" /></li <li><input type="submit" value="提交表单"></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.

So why is the return false in jquery not working, what's the workaround?

Wrote a form to verify the JS code as follows:

?
1234567891011121314 function CheckUserName(){     var username = $("#username").val();     $.get("b.php",{ name:username},       function (data){         if(data == 1){           $("#warnning").html("<font color=#FF3300>Account is used.</font>");           return false//为啥不管用捏?         } else {           $("#warnning").html("<font color=#00CC66>You can register.</font>");           return true//为啥不管用捏?         }       }     );   }

Reason: logic is not clear, to set Ajax to synchronize, you need to use $.ajax,$.get default is asynchronous, and not in the callback function return, but in the Checkusername function to declare a variable to accept the return value of the callback function, Then Checkusername returns this value.
The Modified code:

?
12345678910111213141516  function CheckUserName(){     var username = $("#username").val();     var result=false;     $.ajax({async:false//要设置为同步的,要不CheckUserName的返回值永远为false         ,url:‘b.php‘,data:{name:username}         ,success:function(data){         if(data == 1){           $("#warnning").html("<font color=#FF3300>Account is used.</font>");           result=false;         } else {           $("#warnning").html("<font color=#00CC66>You can register.</font>");           result=true;         }     }});     return result;//==========这里才是CheckUserName的返回值,回调函数返回值没有意义   }

Ok! Test it, no problem!

When to use return in Js/jquery, when to use return false? This is where we are puzzled.

Basically, return is the return of the function, if you need a function to execute the result, return the result you need, do not need the result to return;
There are some special usages in JQ, such as $ (). each (function () {return false;}) ;
If you do not return false, all elements are looped, and if you return false at one time, no subsequent traversal is performed, jumping out of the loop.

The above is for the return of the Jquey in the detailed study, I hope that everyone's learning is helpful.

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.