jquery Each loop break usage

Source: Internet
Author: User

To implement break and continue functions, use a different approach
Break----with return false;
Continue--use return ture;

The code is as follows Copy Code

function MethodOne () {
....
$.each (Array,function () {
if (condition set) {
return true;
}
});
....
}


Each time you want to return to this function with returns True, just let each continue to execute and
There's no interruption to each, so the function can't return.

Let's look at a jquery each instance

jquery each method, in the callback function in accordance with the conditions added a break, jump out of the loop, who knows useless. Finally checked the use of the document, the original to return false in the callback function can be called loop, if you want to continue, return not false on the line.

The code is as follows Copy Code
var len = 0;
Selectedrows.each (function (index) {
if ($jquery (this). Parents ("tr"). Find ("Span:last"). Text () = "process") {
Alert ("Cannot delete the record being processed");
Return false;//equivalent to break;
}
len++;
});


Example Two

The code is as follows Copy Code

$.fn.check = function (mode) {
var mode = Mode | | ' On '; If mode is undefined, use ' on ' as default
Return This.each (function () {
Switch (mode) {
Case ' on ':
This.checked = true;
Break
Case ' off ':
this.checked = false;
Break
Case ' Toggle ':
this.checked =!this.checked;
Break
}
});
};

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.