[英]can't get total value of product items into the total text element
<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); }
Consider the following line: 考虑以下行:
finalTot += $("#itemtot_"+data).val();
val() function returns a string. val()函数返回一个字符串。 To be able to do arithmetic functions on the returned value, you need to convert it to integer using parseInt() function. 为了能够对返回值进行算术运算,您需要使用parseInt()函数将其转换为整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.