<label class="control-label col-xs-5">Total: </label>
<div class="col-xs-6">
<input type="text" name="totalprice" id="finaltotalprice" class="form-control">
</div>
<td>
<input type='text' name='quantity[]' class='form-control' id='quantity_"+ rowId +"' value='"+ orderItems[list].sales_list_item_quantity +"' onchange='calculateItemTotal("+ rowId +")' >
</td>"
<td>
<input type='text' name='discount[]' class='form-control' id='discount_"+ rowId +"' onchange='calculateItemTotal("+ rowId +")' >
</td>
<td>
<!-- this is where the item total price is set and working correctly -->
<input type='text' name='itemtotalprice[]' class='form-control' id='itemtot_"+ rowId +"' >
</td>
function calculateItemTotal(data) {
let quantity = parseInt($("#quantity_"+data).val()); // take the quantity value to quantity variable -- ok
if(isNaN(quantity)) quantity = 0; // make it 0 if it is not a number
let unitPrice = parseFloat($("#unitprice_"+data).val()); // take the unit price value to the unit price variable --ok
if(isNaN(unitPrice)) unitPrice = 0.00;
let tot = quantity * unitPrice; // calculation is ok
let discount = (parseFloat($("#discount_"+data).val())/100 * tot).toFixed(2); // calculation is ok
if(isNaN(discount)) discount = 0.00;
let net_total = tot - discount; // this is also ok
let with2Decimals = net_total.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]; // this is also ok
$("#itemtot_"+data).val(with2Decimals); // set the calculated price of product item -- ok
// something is wrong here!
let finalTot = 0;
finalTot += $("#itemtot_"+data).val();
$("#finaltotalprice").val(finalTot);
}
答案 0 :(得分:0)
请考虑以下行:
finalTot += $("#itemtot_"+data).val();
val()函数返回一个字符串。为了能够对返回值进行算术运算,您需要使用parseInt()函数将其转换为整数。