【问题标题】:Using Jquery 1.8.0 return false is not breaking an each() loop使用 Jquery 1.8.0 return false 不会破坏 each() 循环
【发布时间】:2012-12-05 16:29:46
【问题描述】:

这是我正在使用的脚本。

    $('.row').each(function(){
        $(this).each(function(){
            if($(this).find('.name').val()==""){

                $(this).find('.name').val(data.name);
                $(this).find('.autonomy').val(data.autonomy);
                $(this).find('.purpose').val(data.purpose);
                $(this).find('.flow').val(data.flow);
                $(this).find('.relatedness').val(data.relatedness);
                $(this).find('.challenge').val(data.challenge);
                $(this).find('.mastery').val(data.mastery);
                $(this).find('.colour').val(data.colour);
                done=true;
            }
            if(done==true){
                alert("here");
                return false;
            }
        });
    });

它似乎完全忽略了 return false,我似乎无法弄清楚为什么!

【问题讨论】:

  • 为什么会有两个.each()
  • 确实是这个问题!当天晚些时候解决了:)

标签: jquery loops break each


【解决方案1】:

不需要嵌套each。去掉里面的,return false 就可以了:

$(".row").each(function() {
    // ...
    if (done === true) {
        alert("here");
        return false;
    }
});​

【讨论】:

  • 你不知道他的 HTML 确保他不需要嵌套的 each。
  • @FábioSilva 相信我,没关系。
  • 但也许他想创建一个嵌套循环(对于每一行中的每个元素)。您的解决方案只运行每一行。但我在这种情况下明白你的意思:第二个 this 是 == 到第一个 this,所以他总是访问 row 元素
【解决方案2】:

先向我们展示你的 DOM。

您的返回只会停止第二个 each()。如果要停止第一个,则需要以其他方式进行。

对不起,我不能发表评论,但我的声誉不允许。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 2022-01-23
    • 1970-01-01
    • 2015-03-04
    相关资源
    最近更新 更多