我感覺有點愚蠢,但我無法得到它的工作。我想乘以兩個輸入,然後添加所有總數來獲得總計,但它不起作用,我不明白爲什麼。我一直在關注this one這個主題的幾個主題,但我看不出我做錯了什麼。乘以並添加2個輸入jQuery
$(document).ready(function(){
$('.qtyorder').on("keyup change",function(){
update_amounts();
});
});
function update_amounts(){
var sum = 0.00;
$('#ordertable > tbody > tr').each(function(){
var price = $(this).find('.unitprice').val();
var qty = $(this).find('.qtyorder').val();
var amount = (qty * price)
sum+=amount;
$(this).find('.subtotal').text('' + amount);
});
$('.ordertotal').text(sum);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="order-table" id="ordertable">
<thead>
<tr>
<th class="order-name"></th>
<th class="order-price">Prix</th>
<th class="order-qtytotal">Qty</th>
<th class="order-total">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td class="order-name"><h2>Product1</h2></td>
<td class="order-price">
<input type="text" name="unitprice" class="unitprice" value="30" readonly="readonly" tabindex="-1">
</td>
<td class="order-qtytotal">
<input type="number" class="qtyorder" name="qtyorder" min="0">
</td>
<td class="order-total">
<input type="text" class="subtotal" name="subtotal" readonly="readonly" tabindex="-1" value="0.00">
</td>
</tr>
<tr>
<td class="order-name"><h2>Product2</h2></td>
<td class="order-price">
<input type="text" name="unitprice" class="unitprice" value="40" readonly="readonly" tabindex="-1">
</td>
<td class="order-qtytotal">
<input type="number" class="qtyorder" name="qtyorder" min="0">
</td>
<td class="order-total">
<input type="text" class="subtotal" name="subtotal" readonly="readonly" tabindex="-1" value="0.00">
</td>
</tr>
</tbody>
</table>
<div class="sommeorder">
<label for="ordertotal">Total</label>
<input type="text" class="ordertotal" name="ordertotal" readonly="readonly" tabindex="-1" value="0.00">
</div>
什麼不起作用?你有錯誤嗎? – 2014-10-09 18:31:06
你可能需要用'parseFloat()'來包圍'qty'和'price'。如'var amount =(parseFloat(qty)* parseFloat(price))' – 2014-10-09 18:37:43
我沒有錯誤,它只是不顯示'subtotal'和'ordetotal'輸入中的任何東西... – Pipo 2014-10-09 18:38:57