每个循环停止jquery

时间:2014-01-13 11:30:36

标签: jquery

如何停止$ .each循环?

$.each(files, function(index, file){
    if(file.field_index == remove_file_index){
        //Find remove_file_index Stop $.each
    }
});

Continue to execute code...     

由于

2 个答案:

答案 0 :(得分:12)

您可以return false停止循环

答案 1 :(得分:0)

这是一个演示突破循环的小提琴: http://jsfiddle.net/9XqRy/

// Array
var bar = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Generic function
function loopTest(exit) {
    // jQuery each
    $.each(bar, function (i, o) {
        // Append result to foo div
        $("#foo").append(o + "<br />");
        // If exit is passed, and i is 4, return false
        if (exit && i == 4) {
            return false;
        }
    });
}

// Without exit
loopTest();

// Create a seperator for visual
 $("#foo").append("<br />---<br /><br />");

// With exit
loopTest(true);
相关问题