简体   繁体   English

无法在Ajax计算表格中获得总计

[英]Unable to get grand total in Ajax calculation form

I created a form like this: 我创建了这样的表单:

在此处输入图片说明

The problem is that I am unable to get the grand total of the sub total fields. 问题是我无法获得小计字段的总数。

I tried to create grand total fields same as sub total but no luck. 我试图创建与总计总计相同的总计字段,但是没有运气。 I wonder if I made a mistake not to create some variables globally? 我想知道是否犯了一个错误,那就是不全局创建一些变量? Is there is any simple solution to do that? 有没有简单的解决方案可以做到这一点? You can find the whole code here: http://autopartsdiscountcoupons.com/table/Form.html 您可以在这里找到完整的代码: http : //autopartsdiscountcoupons.com/table/Form.html

 $("#con_qty116").bind('keyup mouseup', function() { var rate = 40; var qty = $("#con_qty116").val(); var val = qty * rate; $("#con_qty116_td").text(val); sub_total_of_weather_sheild(); }); $("#con_qty117").bind('keyup mouseup', function() { var rate = 160; var qty = $("#con_qty117").val(); var val = qty * rate; $("#con_qty117_td").text(val); sub_total_of_decorative_surface(); }); function sub_total_of_weather_sheild() { var td116 = parseInt($("#con_qty116_td").text()); var sub_tot = td116; $("#weather_sheild_qty_sub_total").text(sub_tot); gr_total(sub_tot); } function sub_total_of_decorative_surface() { var td117 = parseInt($("#con_qty117_td").text()); var sub_tot = td117; $("#decorative_surface_qty_sub_total").text(sub_tot); } 
  <td>116</td> <td>Provide and apply Weathershield (Reputable Brand) minimum... </td> <td>Sft</td> <td><input id="con_qty116" name="con_qty116" type="number" /></td> <td>40</td> <td id="con_qty116_td">0</td> <th colspan="4">Sub Total</th> <td id="weather_sheild_qty_sub_total" colspan="2">0</td> <td>117</td> <td>Color Crete min 4mm thickness including surface preparation applied on prepared surface using Applicant Machine/Gun </td> <td>Sft</td> <td><input id="con_qty117" name="con_qty117" type="number" /></td> <td>160</td> <td id="con_qty117_td">0</td> </tr> <tr> <th colspan="4">Sub Total</th> <td id="decorative_surface_qty_sub_total" colspan="2">0</td> </tr> <th colspan="4">Grand Total</th> <th colspan="2" id="grand_total">0</th> 

On each function when you update sub total then you also get every sub total and sum all of these and put in sub total . 在每个功能上,当您更新小计时,您还将获得每个小计,并将所有这些总和相加并放入小计中。 Follow steps :- 遵循步骤:-

  1. Get the value of each sub total in grand_total() function 在grand_total()函数中获取每个小计的值

     function grand_total(){ var grand_total = parseInt($("#ew_qty_sub_total").text()); grand_total += parseInt($("#con_qty_sub_total").text()); grand_total += parseInt($("#rein_qty_sub_total").text()); . . . grand_total += parseInt($("#weather_sheild_qty_sub_total").text()); grand_total += parseInt($("#decorative_surface_qty_sub_total").text()); $("#grand_total").text(grand_total); } 
  2. call this function in your each keyup/mouseup function. 在每个keyup / mouseup函数中调用此函数。 like this:- 像这样:-

     $("#ew_qty1").bind('keyup mouseup',function () { var rate = 12; // Take Quantity from user var qty = $("#ew_qty1").val(); // Multiply Quantity By 10 var val = qty*rate; // Set Text In TD $("#ew_qty1_td").text(val); sub_total_of_earth_Work(); grand_total(); //call for total of sub total }); 

The way you do calculations via script is really dramatic. 通过脚本进行计算的方式确实非常引人注目。 Writing a function for each row in table has no sense - because you ended with more than 100 of function definitions. 为表中的每一行写一个函数没有意义-因为最后有100多个函数定义。

Therefor you have to restructure code and re-use same (one) function to calculate sum. 因此,您必须重组代码并重新使用相同的(一个)函数来计算总和。 Below you can see how it can be done, hope you get an idea: 在下面,您可以看到它是如何完成的,希望您对此有所了解:

 function calculateAll(e) { var grandTotal = 0; $('.sub_table').each(function(i) { var thisTable = $(this); var subTotal = 0; thisTable.find('tbody tr').each(function(j) { var thisRow = $(this); var amount = parseInt(thisRow.find('.multi1').val()) * thisRow.find('.multi2').val(); subTotal += amount; thisRow.find('.amount').val(amount); }); grandTotal += subTotal; thisTable.find('.subtotal').val(subTotal); }); $('.grandtotal').val(grandTotal); }; $('.sub_table input').on('input', calculateAll); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <table class="sub_table"> <thead> <tr> <th>S.No.</th> <th>EARTH WORK</th> <th>Unit </th> <th>Quantity</th> <th>Rate</th> <th>Amount</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Excavation for foundation... </td> <td>Cft</td> <td><input class="multi1" value="0" type="number" /></td> <td><input class="multi2" value="12" readonly /></td> <td><input class="amount" value="0" readonly /></td> </tr> <tr> <td>3</td> <td>Backfilling / Earth Fill... </td> <td>Cft</td> <td><input class="multi1" value="0" type="number" /></td> <td><input class="multi2" value="120" readonly /></td> <td><input class="amount" value="0" readonly /></td> </tr> </tbody> <tfoot> <tr> <th colspan="4">Sub Total</th> <td colspan="2"><input class="subtotal" value="0" readonly /></td> </tr> </tfoot> </table> <table class="sub_table"> <thead> <tr> <th>S.No.</th> <th>EARTH WORK</th> <th>Unit </th> <th>Quantity</th> <th>Rate</th> <th>Amount</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Excavation for foundation... </td> <td>Cft</td> <td><input class="multi1" value="0" type="number" /></td> <td><input class="multi2" value="25" readonly /></td> <td><input class="amount" value="0" readonly /></td> </tr> <tr> <td>3</td> <td>Backfilling / Earth Fill... </td> <td>Cft</td> <td><input class="multi1" value="0" type="number" /></td> <td><input class="multi2" value="18" readonly /></td> <td><input class="amount" value="0" readonly /></td> </tr> </tbody> <tfoot> <tr> <th colspan="4">Sub Total</th> <td colspan="2"><input class="subtotal" value="0" readonly /></td> </tr> </tfoot> </table> <p> Your Grand Total is <input class="grandtotal" value="0" readonly /> </p> 

Also on JSFiddle . 同样在JSFiddle上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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