Home > Article > Web Front-end > Example code sharing of JQuery jumping out of each loop
This article mainly introduces how JQuery jumps out of the each loop. This article explains how jquery jumps out of the current each loop, how Jquery each method jumps out of the loop and gets the return value, etc. Friends who need it can refer to it
1. jquery each loop needs to realize the functions of break and continue:
break----use return false;
continue --use return ture;
2. How to jump out of the current each loop in jquery
Some friends may think that you can directly use continue and break to jump out of the loop in jquery, but it has no effect after using it, because there are no these two commands in jquery.
Later I checked online and got the result:
return false;——Jump out of all loops; equivalent to the break effect in javascript.
return true;——Jump out of the current loop and enter the next loop; equivalent to the continue effect in javascript
Example
$(function (){
$("input[type= 'text']").each(function (i){
var _val=$(this).val();
alert(_val);
if(_val=='2'){
return false; //Jump out of the loop
}
})
});
3. Jquery each method jumps out of the loop And get the return value of the method
return false: will stop the loop (just like using 'break' in a normal loop).
return true: jump to the next loop (just like using 'continue' in a normal loop).
function test(){
var success = false;
$(..).each(function () {
if (..) {
success = true;
return false;
); each() still returns a collection of objects. each(function(){}): It is a callback function. In the callback function, the result cannot be returned outside the callback function each.
The above is the detailed content of Example code sharing of JQuery jumping out of each loop. For more information, please follow other related articles on the PHP Chinese website!