我有一个搜索,但没有找到任何我真正想做的事情.
我有一个动态表单,每个项目有2个输入框,1是一个值,另一个是数量.例如
<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>
Run Code Online (Sandbox Code Playgroud)
所以我需要计算val_1 X val_1_1 = total然后在结尾添加总数(对于每个项目).
我希望我能找到适用于无限项目的东西.
怎么样:jsFiddle示例.
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);?
Run Code Online (Sandbox Code Playgroud)