sum total of values inside dynamic inputs with same class and Auto re-sum total of values when I change any input values or add / delete dynamic row

huangapple go评论57阅读模式
英文:

sum total of values inside dynamic inputs with same class and Auto re-sum total of values when I change any input values or add / delete dynamic row

问题

以下是代码的翻译部分:

<!-- 开始代码段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-js -->
$("#add-btn").click(function() {
  $("#dynamic").append('<tr>' +
    '<td class="td">' +
    '<input type="number" name="Debit" class="form-control Debit"/>' +
    '</td>' +
    '<td class="td">' +
    '<input type="number" class="form-control credit" />' +
    '</td>' +
    '<td class="td2">' +
    '<button type="button" name="add" class="btn btn-danger remove-tr">Remove</button>' +
    '</td>' +
    '</tr>');
});

$(document).on('click', '.remove-tr', function() {
  $(this).parents('tr').remove();
});

$(document).ready(function() {
  var total = 0;
  $('.Debit').each(function() {
    total += parseFloat($(this).val());
    $('.sum_of_Debit').val(total);
  });
  $(".Debit").on("change keyup", function() { 
    var sum = 0;
    sum += parseFloat($('.sum_of_Debit').val());
    $('.sum_of_Debit').val(sum);
  });
});

$(document).ready(function() {
  var total = 0;
  $('.credit').each(function() {
    total += parseFloat($(this).val());
    $('.sum_of_credit').val(total);
  });
  $(".credit").on("change keyup", function() { 
    var sum = 0;
    sum += parseFloat($('.sum_of_credit').val());
    $('.sum_of_credit').val(sum);
  });
});

<!-- 语言:lang-html -->
<table id="dynamic">
  <tr>
    <th class="wd-15p fontColor">credit</th>
    <th class="wd-15p fontColor">Debit</th>
  </tr>
  <tr>
    <td class="td"><input type="number" value="1000" class="form-control credit" /></td>
    <td class="td"><input type="number" value="1000" class="form-control Debit" /></td>
  </tr>
  <tr>
    <td class="td"><input type="number" value="1000" class="form-control credit" /></td>
    <td class="td"><input type="number" value="1000" class="form-control Debit" /></td>
  </tr>
  <tr>
    <td class="td"><input type="number" value="1000" class="form-control credit" /></td>
    <td class="td"><input type="number" value="1000" class="form-control Debit" /></td>
    <td class="td2"><button type="button" name="add" id="add-btn" class="btn btn-success">Add More</button></td>
  </tr>
</table>

<input type="text" class="form-control sum_of_credit" readonly>
<input type="text" class="form-control sum_of_Debit" readonly>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">

<!-- 结束代码段 -->

注意:此代码段主要是用于在表格中添加、删除行以及计算输入框中值的总和。不包含任何其他内容。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;#add-btn&quot;).click(function() {
$(&quot;#dynamic&quot;).append(&#39;&lt;tr&gt;&#39; +
&#39;&lt;td class=&quot;td&quot;&gt;&#39; +
&#39;&lt;input type=&quot;number&quot; name=&quot;Debit&quot; class=&quot;form-control Debit&quot;/&gt;&#39; +
&#39;&lt;/td&gt;&#39; +
&#39;&lt;td class = &quot;td&quot; &gt;&#39; +
&#39;&lt;input type = &quot;number&quot; class = &quot;form-control credit&quot; /&gt;&#39; +
&#39;&lt;/td&gt;&#39; +
&#39;&lt;td class = &quot;td2&quot; &gt;&#39; +
&#39;&lt;button type = &quot;button&quot; name = &quot;add&quot; class = &quot;btn btn-danger remove-tr&quot; &gt; Remove &lt;/button&gt;&#39; +
&#39;&lt;/td&gt;&#39; +
&#39;&lt;/tr&gt;&#39;);
});
$(document).on(&#39;click&#39;, &#39;.remove-tr&#39;, function() {
$(this).parents(&#39;tr&#39;).remove();
});
$(document).ready(function() {
var total = 0;
$(&#39;.Debit&#39;).each(function() {
total += parseFloat($(this).val());
$(&#39;.sum_of_Debit&#39;).val(total);
});
$(&quot;.Debit&quot;).on(&quot;change keyup&quot;, function() { 
var sum = 0;
sum += parseFloat($(&#39;.sum_of_ Debit&#39;).val());
$(&#39;.sum_of_Debit&#39;).val(sum);
});
});
$(document).ready(function() {
var total = 0;
$(&#39;.credit&#39;).each(function() {
total += parseFloat($(this).val());
$(&#39;.sum_of_credit&#39;).val(total);
});
$(&quot;.credit&quot;).on(&quot;change keyup&quot;, function() { 
var sum = 0;
sum += parseFloat($(&#39;.sum_of_credit&#39;).val());
$(&#39;.sum_of_credit&#39;).val(sum);
});
});

<!-- language: lang-html -->

&lt;table id=&quot;dynamic&quot;&gt;
&lt;tr&gt;
&lt;th class=&quot;wd-15p fontColor&quot;&gt;credit&lt;/th&gt;
&lt;th class=&quot;wd-15p fontColor&quot;&gt;Debit&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;td&quot;&gt;&lt;input type=&quot;number&quot; value=&quot;1000&quot; class=&quot;form-control credit&quot; /&gt;&lt;/td&gt;
&lt;td class=&quot;td&quot;&gt;&lt;input type=&quot;number&quot; value=&quot;1000&quot;class=&quot;form-control Debit&quot; /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;td&quot;&gt;&lt;input type=&quot;number&quot; value=&quot;1000&quot; class=&quot;form-control credit&quot; /&gt;&lt;/td&gt;
&lt;td class=&quot;td&quot;&gt;&lt;input type=&quot;number&quot; value=&quot;1000&quot;  class=&quot;form-control Debit&quot; /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;td&quot;&gt;&lt;input type=&quot;number&quot; value=&quot;1000&quot; class=&quot;form-control credit&quot; /&gt;&lt;/td&gt;
&lt;td class=&quot;td&quot;&gt;&lt;input type=&quot;number&quot; value=&quot;1000&quot; class=&quot;form-control Debit&quot; /&gt;&lt;/td&gt;
&lt;td class=&quot;td2&quot;&gt;&lt;button type=&quot;button&quot; name=&quot;add&quot; id=&quot;add-btn&quot; class=&quot;btn btn-success&quot;&gt;Add More&lt;/button&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control sum_of_credit&quot; readonly&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control sum_of_Debit&quot; readonly&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js&quot;&gt;&lt;/script&gt;

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">

<!-- end snippet -->

What is required ??? I want to get the total sum of the values inside the inputs in each of the following cases: When changing any value in the input, the total of values should be re-sum based on the values that were entered. And when adding a new dynamic row and writing new values in the new input this value should be added to the sum total, that is, the total should be recalculated and this new value that was entered should be included in the total sum. When deleting a dynamic row, the sum should be recalculated and the values that were in the deleted row should be excluded, i.e. the value of this row should be deleted from the grand total.

答案1

得分: 1

你的代码部分已经翻译好了。如果有其他问题,请随时提出。

英文:

EDIT: Fixed the issue, event is added to all elements now

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
reCalculate();
$(&quot;#add-btn&quot;).click(function() {
$(&quot;#dynamic&quot;).append(&#39;&lt;tr&gt;&#39; +
&#39;&lt;td class=&quot;td&quot;&gt;&#39; +
&#39;&lt;input type=&quot;number&quot; value= &quot;1000&quot; name=&quot;quantity&quot; class=&quot;form-control quantity&quot;/&gt;&#39; +
&#39;&lt;/td&gt;&#39; +
&#39;&lt;td class = &quot;td&quot; &gt;&#39; +
&#39;&lt;input type = &quot;number&quot; value = &quot;1000&quot; name = &quot;amount&quot; class = &quot;form-control amount&quot;/&gt;&#39; +
&#39;&lt;/td&gt;&#39; +
&#39;&lt;td class = &quot;td2&quot; &gt;&#39; +
&#39;&lt;button type = &quot;button&quot; name = &quot;add&quot; class = &quot;btn btn-danger remove-tr&quot; &gt; Remove &lt;/button&gt;&#39; +
&#39;&lt;/td&gt;&#39; +
&#39;&lt;/tr&gt;&#39;);
reCalculate();
});
// Add on change/keyup to dynamically added elements with event delegation like this
$(&#39;#dynamic&#39;).on(&#39;change keyup&#39;, &#39;.quantity, .amount&#39;, function() {
reCalculate();
});
$(document).on(&#39;click&#39;, &#39;.remove-tr&#39;, function() {
$(this).parents(&#39;tr&#39;).remove();
reCalculate();
});
function reCalculate() {
let totalQty = 0;
let amountQty = 0;
$(&#39;.quantity&#39;).each(function() {
totalQty += parseFloat($(this).val() || 0);
$(&#39;.sum_of_quantity&#39;).val(totalQty);
});
$(&#39;.amount&#39;).each(function() {
amountQty += parseFloat($(this).val() || 0);
$(&#39;.sum_of_amount&#39;).val(amountQty);
});
}
});

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD&quot; crossorigin=&quot;anonymous&quot;&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table id=&quot;dynamic&quot;&gt;
&lt;tr&gt;
&lt;th class=&quot;wd-15p fontColor&quot;&gt;Quantity&lt;/th&gt;
&lt;th class=&quot;wd-15p fontColor&quot;&gt;Amount&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;td&quot;&gt;&lt;input type=&quot;number&quot; value=&quot;1000&quot; name=&quot;quantity[]&quot; class=&quot;form-control quantity&quot; /&gt;&lt;/td&gt;
&lt;td class=&quot;td&quot;&gt;&lt;input type=&quot;number&quot; value=&quot;1000&quot; name=&quot;amount[]&quot; class=&quot;form-control amount&quot; /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;td&quot;&gt;&lt;input type=&quot;number&quot; value=&quot;1000&quot; name=&quot;quantity[]&quot; class=&quot;form-control quantity&quot; /&gt;&lt;/td&gt;
&lt;td class=&quot;td&quot;&gt;&lt;input type=&quot;number&quot; value=&quot;1000&quot; name=&quot;amount[]&quot; class=&quot;form-control amount&quot; /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;td&quot;&gt;&lt;input type=&quot;number&quot; value=&quot;1000&quot; name=&quot;quantity[]&quot; class=&quot;form-control quantity&quot; /&gt;&lt;/td&gt;
&lt;td class=&quot;td&quot;&gt;&lt;input type=&quot;number&quot; value=&quot;1000&quot; name=&quot;amount[]&quot; class=&quot;form-control amount&quot; /&gt;&lt;/td&gt;
&lt;td class=&quot;td2&quot;&gt;&lt;button type=&quot;button&quot; name=&quot;add&quot; id=&quot;add-btn&quot; class=&quot;btn btn-success&quot;&gt;Add More&lt;/button&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;form-control sum_of_quantity&quot; readonly&gt; &lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;form-control sum_of_amount&quot; readonly&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

答案2

得分: 0

将以下内容添加到你的代码中:

$("body").on('keyup change', '.credit, .Debit', function (){
    reCalculate();
});
英文:

add this to your code :

$(&quot;body&quot;).on(&#39;keyup change&#39;, &#39;.credit, .Debit&#39;, function (){
reCalculate();
});

huangapple
  • 本文由 发表于 2023年2月23日 23:59:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定