Ich bin fest mit der Berechnung des Preises für jede Zeile. Wenn ich das Mengenfeld anwähle, wird für beide Zeilen der gleiche Preis gedruckt.Berechnen Sie den Preis für jede Zeile
Hier ist meine Demo https://jsfiddle.net/keyro/fw8t3ehs/2/
Vielen Dank im Voraus.
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()
});
});
Danke alle, Leute. Es funktioniert perfekt. Schätze das! – Khai
@Khai nett! Denken Sie daran, als Antwort und/oder Upvote die Antwort zu markieren, die besser für Sie arbeitet. – DontVoteMeDown