How jquery jumps out of each loop _jquery

Source: Internet
Author: User

One, jquery Each loop, to implement the break and continue function:

Break----with return false;
Continue--use return ture;

Second, how jquery jumps out of the current each loop

Some friends may think that continue and break can be used directly in jquery, but not after use, because there are no two commands in jquery.

Later on the internet to check, got the result:
Return false;--out of all loops, equivalent to the break effect in JavaScript.
Return true;--jump out of the current loop, into the next loop; continue effect in JavaScript
Cases

Copy Code code as follows:

$ (function () {
$ ("input[type= ' text ']"). each (function (i) {
var _val=$ (this). Val ();
alert (_val);
if (_val== ' 2 ') {
return false; Jump out of the loop
}
})
});

Three, Jquery each method jumps out of the loop and gets the return value of the method

return false: The loop will stop (like using ' break ' in an ordinary loop).
Return true: Jumps to the next loop (like using ' Continue ' in an ordinary loop).

Copy Code code as follows:

function Test () {
var success = false;
$(..). each (function () {
If (..) {
Success = true;
return false;
}
});
return success;
}

jquery is the object chain, so $ (..). Each () returns a collection of objects. each (function () {}): is a callback function in which the result cannot be returned outside of the callback function.

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.