Home > Web Front-end > JS Tutorial > body text

How to get out of each loop in JQuery_jquery

WBOY
Release: 2016-05-16 16:03:43
Original
1045 people have browsed it

1. jquery each loop, to realize the break and continue functions:

break----use return false;
continue --use return ture;

2. How to get out of the current each loop in jquery

Some friends may think that you can use continue and break directly to break 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

Copy code The code is 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
}
})
});

3. Jquery each method to jump out of the loop and get the return value

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).

Copy code The code is as follows:

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

jquery is an object chain, so $(..).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.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template