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.