我堅持計算每行的價格。如果我選擇數量字段,它將爲兩行打印相同的價格。計算每一行的價格
這裏是我的演示 https://jsfiddle.net/keyro/fw8t3ehs/2/
預先感謝您。
的Html
<table class="table table-condensed table-hover" id="myTable">
<thead>
<tr>
<th>Product</th>
<th class="text-center">Quantity</th>
<th class="text-right">Price ($)</th>
</tr>
</thead>
<tbody class="body">
<tr data-id="1" data-price="20.00">
<td>Apple ($20)</td>
<td><select class="form-control product_quantities" name="product_quantities[]">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select></td>
<td class="text-right amount" name="amount[]">0</td>
</tr>
<tr data-id="2" data-price="15.00">
<td>Banana ($15)</td>
<td><select class="form-control product_quantities" name="product_quantities[]">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select></td>
<td class="text-right amount" name="amount[]">0</td>
</tr>
<tr>
<td class="text-right bold">TOTAL</td>
<td class="text-center bold">1</td>
<td class="text-right bold"><b class="total">0</b></td>
</tr>
</tbody>
</table>
的Javascript
function totalamount() {
var q = 0;
var rows = document.getElementById('myTable').getElementsByTagName("tbody")[0].getElementsByTagName("tr").length;
for(var i = 0; i < rows; i++){
var z = $('.amount').html();
q +=z;
}
$('.total').html(z);
}
$(function() {
$('.body').delegate('.product_quantities','change',function(){
var tr = $(this).parent().parent();
var qty = tr.find('.product_quantities option:selected').attr('value');
var price = tr.data('price');
var total = (price * qty);
$('.amount').html(total);
totalamount()
});
});
謝謝大家,你們。它完美的作品。讚賞它! – Khai
@ Khai很好!記住標記爲接受和/或upvote答案哪些更適合你。 – DontVoteMeDown