简体   繁体   English

输入框上的jQuery总和

[英]Jquery sum on input boxes

I am using following html form code... 我正在使用以下html表单代码...

<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 .paytax ($('.paytax').val()+$('.payship').val();) but its adding total of first li input to each .paytotal, please help me with proper code. 我正在尝试获取.paytax和.paytax的总和($('。paytax')。val()+ $('。payship')。val();),但是将其添加到每个.paytotal的第一个li输入的总和,请提供正确的代码帮助我。

thanks for support. 感谢你的支持。

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. 我将把整个事情全部解决。

My assumption (without seeing any javascript code) is that you're trying to show the total in each line item. 我的假设(没有看到任何JavaScript代码)是您试图显示每个订单项的总数。 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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM