如何计算每次添加新产品到购物车后的价格?

时间:2013-04-07 13:18:18

标签: jquery action add onchange cart

让我说我有一个推车。

我有价格和数量,给价格的“价格”等级,以及“qty”类的数量跨度。

所以看起来像这样:

<div id="itemsWrapper">
     <div><span class="price">25</span><span class="qty">2</span></div>
     <div><span class="price">40</span><span class="qty">1</span></div>
     ......
</div>

我希望在每次用户将产品添加到购物车后计算最终价格。

这是我试过的价格而不是工作:

$("#itemsWrapper").on("change", "#tableItems", function(){
  var price= new Array();
  $(".price").each(function(index) {
      price.push ($(this).text());
  });
  console.log(price);
});

3 个答案:

答案 0 :(得分:0)

我不确定这是否是你需要的,但我已经尝试了一些代码

var total_price = 0;
$("#itemsWrapper div").each( function() {
    price = $(this).find(".price").html();
    quantity = $(this).find(".qty").html()
    total_price += price * quantity;
});
alert(total_price);

http://jsfiddle.net/cTtyL/

关于连接“已添加项目”的事件,我可能会帮助您更多,但我需要您提供有关如何绑定事件的更多信息。

答案 1 :(得分:0)

我希望在启用FireBug的情况下在您的网站上疯狂购物;)

您不应该使用Javascript计算前端的价格。每次添加项目时发送AJAX请求以改为刷新购物车。

答案 2 :(得分:0)

最后,我得到了我想要的东西。

我在更新HTML购物车后直接插入了这个:

var rtp = new Array();
var qty = new Array();

$(".rtp").each(function() {
   rtp.push ($(this).text());
});

$(".qty").each(function() {
   qty.push ($(this).text());
});
相关问题