如何在 jQuery each() 循环中使用 continue?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17162334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:33:16  来源:igfitidea点击:

How to use continue in jQuery each() loop?

jqueryfor-loopeach

提问by Ravi Kant

In my application i am using AJAX call. I want to use breakand continuein this jQuery loop.

在我的应用程序中,我使用 AJAX 调用。我想在这个 jQuery 循环中使用breakcontinue

$('.submit').filter(':checked').each(function() {

});

回答by Jayram

We can breakboth a $(selector).each()loop and a $.each()loop at a particular iteration by making the callback function return false. Returning non-falseis the same as a continue statement in a forloop; it will skip immediately to the next iteration.

我们可以打破既有$(selector).each()环和一个$.each()通过使回调函数返回在一个特定的迭代循环false。返回non-falsefor循环中的 continue 语句相同;它将立即跳到下一次迭代。

return false; // this is equivalent of 'break' for jQuery loop

return;       // this is equivalent of 'continue' for jQuery loop


Note that $(selector).each()and $.each()are differentfunctions.

请注意,$(selector).each()$.each()不同的功能。

References:

参考:

回答by Suresh Khandekar

$('.submit').filter(':checked').each(function() {
    //This is same as 'continue'
    if(something){
        return true;
    }
    //This is same as 'break'
    if(something){
        return false;
    }
});

回答by Nabil Kadimi

We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration. -- jQuery.each() | jQuery API Documentation

我们可以通过使回调函数返回 false 来在特定迭代中中断 $.each() 循环。返回非 false 与 for 循环中的 continue 语句相同;它将立即跳到下一次迭代。-- jQuery.each() | jQuery API 文档

回答by Billy

return or return false are not the same as continue. If the loop is inside a function the remainder of the function will not execute as you would expect with a true "continue".

return 或 return false 与 continue 不同。如果循环在函数内部,则函数的其余部分将不会像您期望的那样执行真正的“继续”。