0

假设我有一辆购物车。

我有价格和数量,给价格一个“价格”类的跨度,一个“数量”类的数量跨度。

所以它看起来像这样:

<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);
});
4

3 回答 3

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/

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

于 2013-04-07T13:32:43.613 回答
0

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

您不应该使用 Javascript 在前端计算价格。每次添加商品以刷新购物车时发送 AJAX 请求。

于 2013-04-07T13:49:03.750 回答
0

终于,我得到了我想要的。

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

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

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

$(".qty").each(function() {
   qty.push ($(this).text());
});
于 2013-04-07T14:17:12.623 回答