Jquery sum on input boxes
I am using following html form code...
<li><input type="text" class="paytax" name="payment[tax][]" /> <input type="text" class="payship" name="payment[shiping][]" /> <input type="text" class="paytotal" name="payment[total][]" /></li>
<li><input type="text" class="paytax" name="payment[tax][]" /> <input type="text" class="payship" name="payment[shiping][]" /> <input type="text" class="paytotal" name="payment[total][]" /></li>
<li><input type="text" class="paytax" name="payment[tax][]" /> <input type="text" class="payship" name="payment[shiping][]" /> <input type="text" class="paytotal" name="payment[total][]" /></li>
<li><input type="text" class="paytax" name="payment[tax][]" /> <input type="text" class="payship" name="payment[shiping][]" /> <input type="text" class="paytotal" name="payment[total][]" /></li>
I am trying to get total of .paytax and .开发者_JAVA百科paytax ($('.paytax').val()+$('.payship').val();) but its adding total of first li input to each .paytotal, please help me with proper code.
thanks for support.
My assumption (without seeing any javascript code) is that you're trying to show the total in each line item. Tax + Shipping = Total. This code accomplishes that.
$(".paytax,.payship").live("keyup", function() {
var $li = $(this).closest("li");
var tax = parseFloat($li.find(".paytax").val());
var ship = parseFloat($li.find(".payship").val());
if(isNaN(tax)) tax = 0;
if(isNaN(ship)) ship = 0;
$li.find(".paytotal").val(tax + ship);
});
var total = 0;
$('input.paytax, input.payship').each(function() {
total += parseFloat($(this).val());
});
This will total all the inputs of either of those classes.
UPDATE:
I see now what you're trying to do. You want line-by-line totals... Missed that. Go with the other posted answer (Jesse Gavin's). Mine will total the entire thing.
精彩评论