我有这样的HTML更改总和时,变化量
<td class="cart_price">
<p class="price_jq">11</p>
</td>
<td class="cart_quantity">
<input type='button' value='-' class='qtyminus' field='quantity' />
<input type='text' name='quantity' value='1' class='qty' />
<input type='button' value='+' class='qtyplus' field='quantity' />
</td>
<td class="cart_total">
<p class="cart_total_price"></p>
</td>
而这样的jQuery脚本
$(document).ready(function(){
$('.qtyplus').click(function(e){
e.preventDefault();
fieldName = $(this).attr('field');
var currentVal = parseInt($('input[name='+fieldName+']').val());
if (!isNaN(currentVal)) {
$('input[name='+fieldName+']').val(currentVal + 1);
} else {
$('input[name='+fieldName+']').val(0);
}
});
$(".qtyminus").click(function(e) {
e.preventDefault();
fieldName = $(this).attr('field');
var currentVal = parseInt($('input[name='+fieldName+']').val());
if (!isNaN(currentVal) && currentVal > 0) {
$('input[name='+fieldName+']').val(currentVal - 1);
} else {
$('input[name='+fieldName+']').val(0);
}
});
var sum = 0;
var total = 0;
$('.price_jq').each(function() {
var price = $(this);
var count = price.closest('tr').find('.qty');
sum = (price.html() * count.val());
total = total + sum;
price.closest('tr').find('.cart_total_price').append(sum + "₴");
});
$('#total').append("<h3>£" + total + "</h3>");
});
我想我必须使用jQuery的变化,但我尝试和结果,我什么都没有。也许我错过了什么
我需要当我更新td cart_quantity号码,更新我的总价格。
不要删除HTML和代码片段,因为它会使未来的读者无法使用 – Satpal