jQuery,计算字段总和(动态) [英] Jquery, calculate sum of fields (dynamic)
本文介绍了jQuery,计算字段总和(动态)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我进行了搜索,但没有找到我真正想做的任何事情.
I've had a search but haven't found anything that is really what I'm trying to do.
我有一个动态表格,其中每个项目有2个输入框,其中1个是值,另一个是数量. 例如
I have a dynamic form which has 2 input boxes per item, 1 being a value and the other being a quantity. E.g.
<table class="table table-striped table-condensed table-bordered">
<thead>
<tr>
<th>Item</th>
<th width="20%">Value</th>
<th width="20%">Quantity</th>
<th class="actions">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>Item</td>
<td><input type="text" class="input-small" name="var_1" value="0"></td>
<td><input type="text" class="input-small" name="var_1_1" value="0"></td>
<td>$<span class="amount"></span> </td>
</tr>
<tr>
<td>Item</td>
<td><input type="text" class="input-small" name="var_2" value="0"></td>
<td><input type="text" class="input-small" name="var_2_2" value="0"></td>
<td>$<span class="amount"></span> </td>
</tr>
<tr>
<td colspan="3"><strong>Total event cost (viability)</strong></td>
<td><strong>$<div class="total_amount">total</div></strong></td>
</tr>
</tbody>
所以我需要计算val_1 X val_1_1 =总数,然后将总数加到最后(对于每个项目).
So i need to calculate val_1 X val_1_1 = total and then add the total all up at the end (for each item).
我希望我能找到可以用于无限物品的东西.
I was hoping i would find something that will work with unlimited items.
推荐答案
如何操作: jsFiddle示例 .
How about this: jsFiddle example.
jQuery:
function doCalc() {
var total = 0;
$('tr').each(function() {
$(this).find('span.amount').html($('input:eq(0)', this).val() * $('input:eq(1)', this).val());
});
$('.amount').each(function() {
total += parseInt($(this).text(),10);
});
$('div.total_amount').html(total);
}
$('button').click(doCalc);
这篇关于jQuery,计算字段总和(动态)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文