简体   繁体   English

在Jquery中动态创建的输入值相乘和求和

[英]Multiply and sum input values created dynamically in Jquery

I'm creating inputs dynamically in which I want to multiply numbers while I'm typing and the result shows in another input and also the sum of all those results shows in an input with id total . 我正在动态创建输入,我想在输入时将数字相乘,结果显示在另一个输入中,所有这些结果的和也显示在id为total的输入中。

So, this is my code: 所以,这是我的代码:

 <div class="inputs"></div> <button type="button" id="new">NEW</button><br><br> Total:<input id="total" type="text" readonly> <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script> <script> $("#new").click(function(){ var cont = $(".numb").length; var index = cont + 1; var add = "<div><input class='numb' id='nn_"+index+"' onkeyup='calculate("+index+")'> X <input id='mm_"+index+"' onkeyup='calculate("+index+")'> = <input id='result_"+index+"' readonly></div>"; $(".inputs").append(add); }); function calculate(idx){ $("#result_" + idx).val($("#nn_" + idx).val() * $("#mm_" + idx).val()); $("#total").val(Number($("#total").val()) + Number($("#result_" + idx).val())); } </script> 

The problem is when I change numbers, total sum is still going like this case: 问题是当我更改数字时,总和仍然像这种情况:

在此处输入图片说明

It must be 10 and no 14. How can fix it? 它必须是10,而不是14。如何解决?

I'd like some help. 我需要一些帮助。

Part of your issue is that you're cumulatively adding to the total on each change event. 问题的一部分是您将每个变更事件的总和累加到。 You instead need to re-calculate the total entirely based on the new values. 相反,您需要完全根据新值重新计算总数。

You can also make the HTML cleaner by using DOM traversal to relate the elements to each other instead of incremental id attributes as well as using delegated event handlers over the outdated on* event attributes, which should be avoided where possible. 您还可以通过使用DOM遍历将元素彼此关联而不是使用增量id属性,以及通过对过时的on*事件属性使用委派事件处理程序来使HTML变得更整洁,应尽可能避免这样做。 Try this: 尝试这个:

 $("#new").click(function() { $(".inputs").append('<div><input class="number"> X <input class="multi" /> = <input class="line-total" readonly /></div>'); }); $('.inputs').on('input', '.number, .multi', function() { var $container = $(this).closest('div'); var number = $container.find('.number').val() || 0; var multi = $container.find('.multi').val() || 0; $container.find('.line-total').val(number * multi); updateTotal(); }); function updateTotal() { var total = 0; $('.line-total').each(function() { total += parseInt($(this).val(), 10); }); $('#total').val(total); } 
 <div class="inputs"></div> <button type="button" id="new">NEW</button><br><br> Total: <input id="total" type="text" readonly> <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script> 

You need to calculate the sum according to each value and not add every time the new value. 您需要根据每个值计算总和,而不是每次都添加新值。

So what I did : 所以我做了什么:

  • I added a class ".sum" to each result input (to select them) 我向每个结果输入添加了一个“ .sum”类(以选择它们)

  • I made 2 functions : one to calculate the result of the row + one for the sum 我做了2个函数:一个用于计算行的结果+一个用于求和

  • I added the new function on keyup event 我在keyup事件中添加了新功能

So the logic is, when you change an input value : 因此,逻辑是,当您更改输入值时:

  • the result of the row change (as before) 行更改的结果(与以前一样)
  • you calculate the sum according to each result input, starting at 0 (new) 您可以根据每个结果输入计算总和,从0(新)开始

 <div class="inputs"></div> <button type="button" id="new">NEW</button><br><br> Total:<input id="total" type="text" readonly> <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script> <script> $("#new").click(function(){ var cont = $(".numb").length; var index = cont + 1; var add = "<div><input class='numb' id='nn_"+index+"' onkeyup='calculate("+index+"); total()'> X <input id='mm_"+index+"' onkeyup='calculate("+index+"); total()'> = <input class='sum' id='result_"+index+"' readonly></div>"; $(".inputs").append(add); }); function calculate(idx){ $("#result_" + idx).val($("#nn_" + idx).val() * $("#mm_" + idx).val()); } function total() { var sum = 0; $.each($(".sum"), function() { sum = sum + Number($(this).val()); }); $("#total").val(sum); } </script> 

Hope it helps ! 希望能帮助到你 !

When you update a cell calculate(idx) is recalculating the row, but for the total it is not recalculating, it is adding the new row's total to the previously calculated total that alrady had added the old value. 更新单元格时, calculate(idx)正在重新计算该行,但对于总数却没有重新计算,它将新行的总数与alrady添加了旧值的先前计算的总数相加。

To correct it you need to reset the total to 0 and loop through all rows to re-add all the row totals. 要更正它,您需要将总数重置为0并遍历所有行以重新添加所有行的总数。

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

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