从jQuery中的“running total”中删除项目

时间:2010-11-02 16:37:46

标签: jquery math

这是我的购物车代码(到目前为止的总数,但最终是购物车)我正在尝试将代码放入减去“总计”中点击项目的值,这是我的代码:< / p>

$('.addtocart').click(function(){
                $('#cart').show();
                var omPartNo = $(this).next().text();
                var supPartNo = $(this).next().next().text();
                var cat = $(this).next().next().next().text();
                var desc = $(this).next().next().next().next().text();
                var manuf = $(this).next().next().next().next().next().text();
                var list = $(this).next().next().next().next().next().next().text();
                var disc = $(this).next().next().next().next().next().next().next().text();
                var priceEach = $(this).next().next().next().next().next().next().next().next().text();
                $('#cart table').append('<tr class="tableRow"><td><a class="removeItem" href="#"><img src="/admin/images/delete.png"></img></a><td>' + omPartNo + '</td><td>' + supPartNo + '</td><td>' + cat + '</td><td>' + desc + '</td><td>' + manuf + '</td><td>' + list + '</td><td>' + disc + '</td><td class="price">' + priceEach + '</td></tr>');
                    var total = 0;
                    $('.price').each(function() { 
                        total += parseFloat($.text([this]));
                    });
                    $('#subtotal span').html(total.toFixed(2));

            });

            $('.removeItem').live('click',function(){                                       
                $(this).closest('tr').remove(); 
            }); 

所以,当我点击.removeItem时,如果可能,我需要从“小计跨度”中减去?

由于

2 个答案:

答案 0 :(得分:0)

创建一个计算总数的函数,并在有理由重新计算时调用它。

 function calcTotal(){
     var total = 0;
     $('.price').each(function() { 
         total += parseFloat($(this).text());
       });
     $('#subtotal span').html(total.toFixed(2));
  }

现在改变

$('#cart table').append('<tr class="tableRow"><td><a class="removeItem" href="#"><img src="/admin/images/delete.png"></img></a><td>' + omPartNo + '</td><td>' + supPartNo + '</td><td>' + cat + '</td><td>' + desc + '</td><td>' + manuf + '</td><td>' + list + '</td><td>' + disc + '</td><td class="price">' + priceEach + '</td></tr>');
                    var total = 0;
                    $('.price').each(function() { 
                        total += parseFloat($.text([this]));
                    });
                    $('#subtotal span').html(total.toFixed(2));

            });

            $('.removeItem').live('click',function(){                                       
                $(this).closest('tr').remove(); 
            }); 

$('#cart table').append('<tr class="tableRow"><td><a class="removeItem" href="#"><img src="/admin/images/delete.png"></img></a><td>' + omPartNo + '</td><td>' + supPartNo + '</td><td>' + cat + '</td><td>' + desc + '</td><td>' + manuf + '</td><td>' + list + '</td><td>' + disc + '</td><td class="price">' + priceEach + '</td></tr>');
                    calcTotal();

            });

            $('.removeItem').live('click',function(){                                       
                $(this).closest('tr').remove(); 
                calcTotal();
            }); 

答案 1 :(得分:0)

var tableRow = $(this).closest('tr');
var price = parseFloat(tableRow.find('.price').text());
total -= price.toFixed(2);

tableRow.remove();