如何跳出jQuery每个循环

时间:2022-08-24 00:05:44

How do I break out of a jQuery each loop?

如何跳出一个jQuery循环?

I have tried:

我有尝试:

 return false;

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

在循环中,但这不起作用。什么好主意吗?

UPDATE While it's not clear what the original scope of the question was, please beware that the chosen answer only covers the $.each() function but not the $().each.

更新虽然还不清楚问题的初始范围是什么,但请注意,选择的答案只包含$.each()函数,而不是$()。

5 个解决方案

#1


860  

To break a $.each loop, you have to return false in the loop callback.

打破美元。每个循环,都必须在循环回调中返回false。

Returning true skips to the next iteration, equivalent to a continue in a normal loop.

返回真正的跳转到下一个迭代,相当于正常循环中的继续。

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

Break out of a $().each loop is covered in How to break/exit from a each() function in JQuery?

从$()中取出。每个循环都包含在JQuery中如何从每个()函数中中断/退出?

#2


51  

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

根据文件返回错误;应该做这项工作。

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

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

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, continue works like this:

顺便说一句,继续这样的工作:

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语句相同;它将立即跳到下一个迭代。

#3


31  

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

我创建了一个小提琴为这个问题的答案因为接受的答案是不正确的加上这是第一个*线程从谷歌返回关于这个问题。

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

从$中取出。每一个你必须使用返回错误;

Here is a Fiddle proving it:

这里有一个小提琴证明:

http://jsfiddle.net/9XqRy/

http://jsfiddle.net/9XqRy/

#4


23  

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;
} 

#5


8  

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

“每一个”使用回调函数。回调函数不考虑调用函数执行,因此不可能从回调函数返回到调用函数。

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

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

#1


860  

To break a $.each loop, you have to return false in the loop callback.

打破美元。每个循环,都必须在循环回调中返回false。

Returning true skips to the next iteration, equivalent to a continue in a normal loop.

返回真正的跳转到下一个迭代,相当于正常循环中的继续。

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

Break out of a $().each loop is covered in How to break/exit from a each() function in JQuery?

从$()中取出。每个循环都包含在JQuery中如何从每个()函数中中断/退出?

#2


51  

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

根据文件返回错误;应该做这项工作。

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

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

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, continue works like this:

顺便说一句,继续这样的工作:

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语句相同;它将立即跳到下一个迭代。

#3


31  

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

我创建了一个小提琴为这个问题的答案因为接受的答案是不正确的加上这是第一个*线程从谷歌返回关于这个问题。

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

从$中取出。每一个你必须使用返回错误;

Here is a Fiddle proving it:

这里有一个小提琴证明:

http://jsfiddle.net/9XqRy/

http://jsfiddle.net/9XqRy/

#4


23  

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;
} 

#5


8  

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

“每一个”使用回调函数。回调函数不考虑调用函数执行,因此不可能从回调函数返回到调用函数。

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

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