jQuery 如何跳出jQuery每个循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1784780/
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 12:07:43  来源:igfitidea点击:

How to break out of jQuery each Loop

jqueryloops

提问by Luke101

How do I break out of a jQuery eachloop?

如何跳出 jQueryeach循环?

I have tried:

我试过了:

 return false;

In the loop but this did not work. Any ideas?

在循环中,但这不起作用。有任何想法吗?

回答by CMS

To breaka $.eachor $(selector).eachloop, you have to return falsein the loop callback.

break一个$.each$(selector).each循环,你必须返回false在循环回调。

Returning trueskips to the next iteration, equivalent to a continuein a normal loop.

Returningtrue跳到下一次迭代,相当于continue普通循环中的 a。

$.each(array, function(key, value) { 
    if(value === "foo") {
        return false; // breaks
    }
});

// or

$(selector).each(function() {
  if (condition) {
    return false;
  }
});

回答by powtac

According to the documentation return false;should do the job.

根据文档return false;应该完成这项工作。

We can break the $.each() loop [..] by making the callback function return false.

我们可以通过使回调函数返回 false 来打破 $.each() 循环 [..]。

Return false in the callback:

在回调中返回 false:

function callback(indexInArray, valueOfElement) {
  var booleanKeepGoing;

  this; // == valueOfElement (casted to Object)

  return booleanKeepGoing; // optional, unless false 
                           // and want to stop looping
}

BTW, continueworks like this:

顺便说一句,continue工作是这样的:

Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

返回非 false 与 for 循环中的 continue 语句相同;它将立即跳到下一次迭代。

回答by iamjpg

I created a Fiddle for the answer to this question because the accepted answer is incorrect plus this is the first StackOverflow thread returned from Google regarding this question.

我为这个问题的答案创建了一个 Fiddle,因为接受的答案是不正确的,而且这是 Google 就这个问题返回的第一个 StackOverflow 线程。

To break out of a $.each you must use return false;

要突破 $.each,您必须使用 return false;

Here is a Fiddle proving it:

这是一个证明它的小提琴:

http://jsfiddle.net/9XqRy/

http://jsfiddle.net/9XqRy/

回答by David Aguirre

I came across the situation where I met a condition that broke the loop, however the code after the .each() function still executed. I then set a flag to "true" with an immediate check for the flag after the .each() function to ensure the code that followed was not executed.

我遇到了这样的情况,我遇到了打破循环的条件,但是 .each() 函数之后的代码仍然执行。然后我将一个标志设置为“true”,并在 .each() 函数之后立即检查该标志以确保后面的代码没有被执行。

$('.groupName').each(function() {
    if($(this).text() == groupname){
        alert('This group already exists');
        breakOut = true;
        return false;
    }
});
if(breakOut) {
    breakOut = false;
    return false;
} 

回答by Sumit Jambhale

"each" uses callback function. Callback function execute irrespective of the calling function,so it is not possible to return to calling function from callback function.

“each”使用回调函数。回调函数的执行与调用函数无关,因此不可能从回调函数返回到调用函数。

use for loop if you have to stop the loop execution based on some condition and remain in to the same function.

如果您必须根据某些条件停止循环执行并保持在同一函数中,请使用 for 循环。

回答by Vural

I know its quite an old question but I didn't see any answer, which clarify that why and when its possible to break with return.

我知道这是一个很老的问题,但我没有看到任何答案,这澄清了为什么以及何时可能与 return 中断。

I would like to explain it with 2 simple examples:

我想用两个简单的例子来解释它:

1. Example:In this case, we have a simple iteration and we want to break with return true, if we can find the three.

1. 示例:在这种情况下,我们有一个简单的迭代,如果我们能找到三个,我们想用 return true 来中断。

function canFindThree() {
    for(var i = 0; i < 5; i++) {
        if(i === 3) {
           return true;
        }
    }
}

if we call this function, it will simply return the true.

如果我们调用这个函数,它会简单地返回 true。

2. ExampleIn this case, we want to iterate with jquery's each function, which takes anonymous function as parameter.

2. 示例在这种情况下,我们要迭代 jquery 的each 函数,它以匿名函数为参数。

function canFindThree() {

    var result = false;

    $.each([1, 2, 3, 4, 5], function(key, value) { 
        if(value === 3) {
            result = true;
            return false; //This will only exit the anonymous function and stop the iteration immediatelly.
        }
    });

    return result; //This will exit the function with return true;
}