对于SO社区,我有一个脚本,它将具有唯一名称的行添加到表中。我想在每一行上输入两个输入并将它们相乘并在第三个输入中显示结果。jquery在动态表中对两个输入进行求和并在第三个显示
小提琴在http://jsfiddle.net/yUfhL/231/
我的代码是: HTML
<table class="order-list">
<tr><td>Product</td><td>Price</td><td>Qty</td><td>Total</td></tr>
<tr>
<td><input type="text" name="product" /></td>
<td><input type="text" name="price" /></td>
<td><input type="text" name="qty" /></td>
<td><input type="text" name="linetotal" /></td>
</tr>
</table>
<div id="grandtotal">
Grand Total Here
</div>
JS
var counter = 1;
jQuery("table.order-list").on('change','input[name^="product"]',function(event){
event.preventDefault();
counter++;
var newRow = jQuery('<tr><td><input type="text" name="product' +
counter + '"/></td><td><input type="text" name="price' +
counter + '"/></td><td><input type="text" name="qty' +
counter + '"/></td><td><input type="text" name="total' +
counter + '"/></td><td><a class="deleteRow"> x </a></td></tr>');
jQuery('table.order-list').append(newRow);
});
jQuery("table.order-list").on('click','.deleteRow',function(event){
$(this).closest('tr').remove();
});
$('table.order-list tr').each(function() {
var price = parseInt($('input[id^=price]', this).val(), 10);
var qty = parseInt($('input[id^=qty]' , this).val(), 10);
$('input[id^=linetotal]', this).val(price * qty);
});
所以目前我不能让JS把大火,得到两个电池的产品,数量和价格。我想在linetotal中显示结果。
的这最后一部分将在div grandtotal
帮助理解,因为总是显示所有linetotals作为总计的总和。
你只给'''元素一个'name'属性,但使用'id'选择符('[id^= price]')。给他们一个特定的课程要比使用“id开始于”选择器要容易得多。另外,您希望何时计算行总数?你想什么时候计算总计?什么事件? – Ian 2013-03-27 20:26:41