如何跳出 jQuery 每个循环?

发布于 2025-01-11 11:16:31 字数 276 浏览 0 评论 0原文

如何打破 jQuery each 循环?

我已经尝试过:

 return false;

在循环中但这不起作用。有什么想法吗?


2020 年 9 月 5 日更新

我将 return false; 放在了错误的位置。当我将它放入循环中时,一切正常。

How do I break out of a jQuery each loop?

I have tried:

 return false;

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


Update 9/5/2020

I put the return false; in the wrong place. When I put it inside the loop everything worked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

梦晓ヶ微光ヅ倾城 2025-01-18 11:16:31

破坏 $.each$(selector).each 循环,您必须返回 false 在循环回调中。

返回 true 会跳到下一次迭代,相当于正常循环中的 continue 。

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

// or

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

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

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; // breaks
    }
});

// or

$(selector).each(function() {
  if (condition) {
    return false;
  }
});
-黛色若梦 2025-01-18 11:16:31

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

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

在回调中返回 false:

function callback(indexInArray, valueOfElement) {
  var booleanKeepGoing;

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

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

顺便说一句,继续 的工作方式如下:

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

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

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

Return false in the callback:

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.

如歌彻婉言 2025-01-18 11:16:31

我遇到过这样的情况:我遇到了打破循环的条件,但是 .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;
} 

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.

$('.groupName').each(function() {
    if($(this).text() == groupname){
        alert('This group already exists');
        breakOut = true;
        return false;
    }
});
if(breakOut) {
    breakOut = false;
    return false;
} 
云巢 2025-01-18 11:16:31

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

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

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

http://jsfiddle.net/9XqRy/

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.

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

Here is a Fiddle proving it:

http://jsfiddle.net/9XqRy/

旧城空念 2025-01-18 11:16:31

我知道这是一个相当老的问题,但我没有看到任何答案,这澄清了为什么以及何时可以打破返回。

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

1.示例:
在本例中,我们有一个简单的迭代,如果我们能找到这三个,我们希望返回 true 来中断。

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

如果我们调用此函数,它将简单地返回 true。

2.示例
在本例中,我们希望使用 jquery 的 每个函数 进行迭代,该函数采用匿名函数作为参数。

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

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.

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.

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.

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

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;
}
就像说晚安 2025-01-18 11:16:31

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

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

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

万劫不复 2025-01-18 11:16:31

我使用这种方式(例如):

$(document).on('click', '#save', function () {
    var cont = true;
    $('.field').each(function () {
        if ($(this).val() === '') {
            alert('Please fill out all fields');
            cont = false;
            return false;
        }
    });
    if (cont === false) {
        return false;
    }
    /* commands block */
});

如果 cont 不是 false 则运行命令块

I use this way (for example):

$(document).on('click', '#save', function () {
    var cont = true;
    $('.field').each(function () {
        if ($(this).val() === '') {
            alert('Please fill out all fields');
            cont = false;
            return false;
        }
    });
    if (cont === false) {
        return false;
    }
    /* commands block */
});

if cont isn't false runs commands block

纵性 2025-01-18 11:16:31

>我们可以使用break关键字来打破循环。

Example:

for (let i = 0; i < 10; i++) {
  if (i === 3) { break; }
  text += "The number is " + i + "<br>";
}

> We can break the loop by using break keyword.

Example:

for (let i = 0; i < 10; i++) {
  if (i === 3) { break; }
  text += "The number is " + i + "<br>";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文