我如何向页面上的现有数字添加总和?
使用.empty()
然后通过jQuery进行求和并在.html()
中再次设置它会更好吗?
实施例
<div class="price">$200</div>
然后jQuery会将$50
添加到现有价格,新价格将为$250
。
根据广播选择Yes
添加$50
而No
只是保留原样。
如果他们选择返回和第四,会有什么方法可以撤消最后一次添加?
答案 0 :(得分:3)
将价格存储在元素中:
<div class="product" data-price="50.0">I'm a product</div>
只要有变化,就把它们添加起来:
var total = 0;
$('.product').filter(function() {
// Remove the ones that aren't checked or something
return true;
}).each(function() {
total += $(this).data('price');
});
$('.price').text('$' + total);