简体   繁体   English

使用js或jQuery计算td类的总和

[英]Calculating sum of td classes using js or jQuery

I have a dropdown select menu with various options (ie 5, 10, 15, 20...) that represents # of computers. 我有一个下拉选择菜单,其中包含代表计算机数量的各种选项(即5,10,15,20 ......)。 The default select menu value is 5. I am using some js to multiply the dropdown selection by an amount (ie 10) and populates a table td with a class of .price-1. 默认的选择菜单值是5.我使用一些js将下拉选项乘以一个数量(即10),并使用.price-1类填充表td。 So, for example if the user leaves the default selection of 5, the calculated value of .price-1 is 50. 因此,例如,如果用户保留默认选择5,则.price-1的计算值为50。

This is working fine. 这工作正常。

However, I then need to sum .price-1 with a few other <td> classes (ie .price-2, .price-3, .price-4...) to get a grand total in $ values that shows in #result. 但是,我需要将.price-1与其他一些<td>类(即.price-2,.price-3,.price-4 ...)相加,以得到$ value中的总值。 #结果。

How can I use js or jQuery to sum these td classes to get the grand total? 我如何使用js或jQuery来总结这些td类来获得总计?

Below is my html of my table I need to sum. 下面是我需要总结的表格的html。

<table id="tableOrderTotal" class="table tableTotal"> 
 <tbody> 
  <tr> 
   <td>Item1</td> 
   <td class="price-1">calculated amount populated here</td> 
  </tr> 
  <tr> 
   <td>Item2</td> 
   <td class="price-2">calculated amount populated here</td> 
  </tr> 
  <tr> 
   <td>Item3</td> 
   <td class="price-3">13</td> 
  </tr>
  <tr> 
   <td>Item3</td> 
   <td class="price-4">30</td> 
  </tr> 
  <tr class="summary"> 
   <td class="totalOrder">Total:</td> 
   <td id="result" class="totalAmount"> </td> 
  </tr> 
 </tbody> 
</table>

Get all td elements either using attribute value contains selector or by second td element of tr using :nth-child() . 使用:nth-child()使用属性值包含选择器tr的第二个td元素获取所有td元素。 Now iterate over them using each() method and get sum using the text inside. 现在使用each()方法迭代它们并使用里面的文本获得总和。

 var sum = 0; $('td[class*="price-"]').each(function() { sum += Number($(this).text()) || 0; }); $('#result').text(sum); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="tableOrderTotal" class="table tableTotal"> <tbody> <tr> <td>Item1</td> <td class="price-1">calculated amount populated here</td> </tr> <tr> <td>Item2</td> <td class="price-2">calculated amount populated here</td> </tr> <tr> <td>Item3</td> <td class="price-3">13</td> </tr> <tr> <td>Item3</td> <td class="price-4">30</td> </tr> <tr class="summary"> <td class="totalOrder">Total:</td> <td id="result" class="totalAmount"></td> </tr> </tbody> </table> 


With Array#reduce method as @rayon suggested. 使用Array#reduce方法作为@rayon建议。

 $('#result').text([].reduce.call($('td[class*="price-"]'), function(sum, ele) { return sum + (Number($(ele).text()) || 0); }, 0)); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="tableOrderTotal" class="table tableTotal"> <tbody> <tr> <td>Item1</td> <td class="price-1">calculated amount populated here</td> </tr> <tr> <td>Item2</td> <td class="price-2">calculated amount populated here</td> </tr> <tr> <td>Item3</td> <td class="price-3">13</td> </tr> <tr> <td>Item3</td> <td class="price-4">30</td> </tr> <tr class="summary"> <td class="totalOrder">Total:</td> <td id="result" class="totalAmount"></td> </tr> </tbody> </table> 

jQuery Object has a direct attribute referring to the number of the matched elements. jQuery Object有一个直接属性,指的是匹配元素的数量。

var sum = $('td[class*="price-"]').length;
$('#result').text(sum);

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

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