【问题标题】:How can I iterate over 2 arrays at the same time using $.each()如何使用 $.each() 同时迭代 2 个数组
【发布时间】:2011-11-11 08:32:56
【问题描述】:

我有一个名为 prices 的数组和另一个名为 orderT 的数组)。

到目前为止,我可以遍历价格(使用 accounting.js 插件):

function processTotal(){
        var prices = $('.prices');
        var orderT = $('.orderType');
        var total = 0;
        $.each(prices, function(i, e){
            total += accounting.unformat(prices[i].textContent);
        });

        total = parseFloat(total);
        $('#sum').text(accounting.formatMoney(total, "€", 2, ".", ","));
    }

价格部分效果很好,但我不知道如何通过orderT 进行检查。

编辑

orderTprices 是相关的并且在一个表中,我无法发布所有表,因为它很大但基本上是:

<td>order_type</td>
<td>$00.00</td>

【问题讨论】:

  • 使用普通的for 循环?
  • 试过了,奇怪的事情发生了,但也许我只是做错了什么?

标签: jquery iteration each


【解决方案1】:

试试这个(假设 .prices 的数量与 .orderType 的数量相同):

$.each(prices, function(i, e){             
    var orderType = orderT [i];
    if(orderType.textContent == "2"){
        //Do Something
    } else {
        //total += accounting.unformat(prices[i].textContent);         
        //Do Something
    }
}); 

看起来 .price 和 .orderType 是相关的。如果您可以发布 HTML,则可以改进此代码以使用兄弟/其他选择器来访问 .price 和相应的 .orderType

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 2022-01-15
    • 2015-05-26
    • 2011-08-24
    • 1970-01-01
    相关资源
    最近更新 更多