Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法中断jquery的每个循环_Jquery_Return_Iteration_Each - Fatal编程技术网

无法中断jquery的每个循环

无法中断jquery的每个循环,jquery,return,iteration,each,Jquery,Return,Iteration,Each,我在jquery中编写了一个嵌套循环,在子循环中返回false,并将相同的文本追加到父行。我的代码 $('#listingProducts ul.msRows li.msFullfillment').each(function(index) { if(typeof $('#hfOrd'+index).val() != 'undefined'){ var $this = $(this); var orderId = $

我在jquery中编写了一个嵌套循环,在子循环中返回false,并将相同的文本追加到父行。我的代码

$('#listingProducts ul.msRows li.msFullfillment').each(function(index) {                    
    if(typeof $('#hfOrd'+index).val() != 'undefined'){
        var $this = $(this);
        var orderId = $('#hfOrd'+index).val();                      
        // repainting logic
        $('body').append(data);
        $('#ajaxProducts ul.displayPoints li').each(function(index){
            var $child = $(this);
            if(typeof $('#hfAjaxOrderId'+index).val() != 'undefined'){
                var ajaxOrderId = $('#hfAjaxOrderId'+index).val();
                //alert(orderId+' '+ ' '+ajaxOrderId);
                if(ajaxOrderId === orderId){
                    // replace the div here..
                    var anchorText = $child.find("#pointsLineAjax .redeem").text();     
                    $this.find("#pointsLine .redeem").text(anchorText);
                    return false;
                }
            }
        });

    }
});

在子循环内返回false不会返回到父循环。这似乎并没有将其写入相应的行。我错过了什么

返回
false
只会在jQuery循环中中断内部循环,原因有很好的解释

这里的问题是,虽然您可以从 .每个回调,.each函数本身返回jQuery对象。 因此,您必须在两个级别上都返回false以停止 循环。也因为没有办法知道自己的内心。每一个都找到了 无论是否匹配,我们都必须使用一个使用闭包的共享变量 这会得到更新

请尝试以下操作:

$('#listingProducts ul.msRows li.msFullfillment').each(function(index) {                    
    var continueLoop = true;
    if($('#hfOrd'+index).length){
        var $this = $(this);
        var orderId = $('#hfOrd'+index).val();                      
        // repainting logic
        $('body').append(data);
        $('#ajaxProducts ul.displayPoints li').each(function(index){
            var $child = $(this);
            if($('#hfAjaxOrderId'+index).length){
                var ajaxOrderId = $('#hfAjaxOrderId'+index).val();
                //alert(orderId+' '+ ' '+ajaxOrderId);
                if(ajaxOrderId === orderId){
                    // replace the div here..
                    var anchorText = $child.find("#pointsLineAjax .redeem").text();     
                    $this.find("#pointsLine .redeem").text(anchorText);
                    continueLoop = false;
                    return false;
                }
            }
        });
    };
    return continueLoop;
});

什么是
$('#hfOrd'+index).val()
?创建一个提琴来检查发生了什么。@ling.s它是现有行中的orderid,我正在与从ajax返回的具有orderid的行进行比较,如果两者相同,我正在替换文本。为什么要使用+index?@ling.s这就是它的含义,它只是将文本追加到文本以生成选择器: