简体   繁体   English

无法将产品项目的总价值计入总文本元素

[英]can't get total value of product items into the total text element

  1. I have list of product item which generated dynamically. 我有动态生成的产品清单。
  2. I calculated each item total using calculateItemTotal() method. 我使用calculateItemTotal()方法计算了每个项目的总和。
  3. what I wanted to do is take the sum of all these item totals in to the total text field. 我想做的是将所有这些项目的总和输入到文本字段中。
  4. but, instead of summing item totals, total text field only shows the last item total price. 但是,“总计”文本字段仅显示最后一个项目的总价,而不是对项目总计进行汇总。
  5. If anyone can give me insight , How to figure this out would be great. 如果有人可以给我深刻的见解,那么如何解决这个问题将是很棒的。

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> 

Dynamically added rows 动态添加的行

  • rowId is already defined in the script...no problem with the rowId. rowId已在脚本中定义... rowId没问题。
  • onchange() functions are working correctly as well. onchange()函数也正常工作。

 <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> 


This is my calculateItemTotal() function 这是我的calculateItemTotal()函数

 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); } 


Here is the image of what I'm getting 这是我所得到的图像

总数应该是1780

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM