计算子总数和总jquery [英] calculate sub total and total jquery
本文介绍了计算子总数和总jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张表格,其中包含发票数量,单价和小计等字段。我想循环并得到每个输入字段的子总数,最后得到子总和的总和。我怎么能在JQuery上做到这一点?谢谢。
I have a form with fields like invoice Quantity, Unit Price and Sub total. I want to loop through and get sub total of each input field and finally get sum of sub total. How can I do that on JQuery? Thanks.
我的代码:
$(document).keyup(function(event) {
var sub_total = 0;
$("#items .targetfields").each(function() {
var qty = parseInt($("#quantity").val());
var rate = parseInt($("#rate").val());
$(".subtotal").val(qty * rate);
});
});
推荐答案
我修复了你的一些html(从不使用重复的id )
I fixed some of your html (never use duplicate ids)
<table id="items">
<tr class="targetfields">
<td><input type="text" name="quantity" class="quantity" /></td>
<td><input type="text" name="rate" class="rate" size="5" /></td>
<td><input type="text" name="sub_total" class="subtotal" /></td>
</tr>
<tr class="targetfields">
<td><input type="text" name="quantity" class="quantity" /></td>
<td><input type="text" name="rate" class="rate" size="5" /></td>
<td><input type="text" name="sub_total" class="subtotal" /></td>
</tr>
</table>
total: <span id="total"></span>
这里是js
$(function() {
$("#items").keyup(function(event) {
var total = 0;
$("#items .targetfields").each(function() {
var qty = parseInt($(this).find(".quantity").val());
var rate = parseInt($(this).find(".rate").val());
var subtotal = qty * rate;
$(this).find(".subtotal").val(subtotal);
if(!isNaN(subtotal))
total+=subtotal;
});
$("#total").html(total);
});
})
这篇关于计算子总数和总jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文