如何使用 jQuery 将数组元素添加到自身



我有这个特定的代码

var added_products_array = [];
$('.product tr').each(function(){
    var added_products_row = [];
    added_products_row.push($(this).find('.product_id').html());
    added_products_row.push($(this).find('.productName').html());
    added_products_row.push($(this).find('.quantity').html());
    added_products_row.push($(this).find('.units').html());
    added_products_row.push($(this).find('.unitPrice').html());
    added_products_row.push($(this).find('.discount').html());
    added_products_row.push($(this).find('.totalAmount').html());
    added_products_array.push(added_products_row);
    $.each([added_products_row[2]], function( index, value ) {  
        var store = 0;
        store += parseInt($(this));
        alert(store);
    });
});

.product 是 tbody,其中的 tr 是从另一个div 自动生成的,它给出了你在上面看到的值推送到数组中。

我试图使用您在上面看到的每个函数将数量中的所有 TD 值相加,但我似乎无法使其工作。 有什么帮助吗?

每次在

循环中初始化数组和其他变量。此外,我还删除了另一个循环并将其保留在第一个循环之后。让我知道这是否是您要找的。

尝试这样的事情,

var added_products_row = [];
$('.product tr').each(function(){
        added_products_row.push($(this).find('.product_id').html());
        added_products_row.push($(this).find('.productName').html());
        added_products_row.push($(this).find('.quantity').html());
        added_products_row.push($(this).find('.units').html());
        added_products_row.push($(this).find('.unitPrice').html());
        added_products_row.push($(this).find('.discount').html());
        added_products_row.push($(this).find('.totalAmount').html());
        added_products_array.push(added_products_row);
    });
var store = 0;
$.each([added_products_row[2]], function( index, value ) {          
            store += parseInt(value);
});
alert(store);

最新更新