對於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