简体   繁体   English

带有HTML表的jQuery Math

[英]Jquery Math with an HTML Table

so I have a table that's like the following. 所以我有一个如下表。

 <table> <tr><th>My Table><th class="total"></th></tr> <tr><td class="title">title 1</td><td class="value">10%</td></tr> <tr><td class="title">title 1</td><td class="value">20%</td></tr> <tr><td class="title">title 1</td><td class="value">30%</td></tr> <tr><td class="title">title 1</td><td class="value">40%</td></tr> </table> 

I'm wanting to calculate the sum of all td's with the "value" class (which should equal 100). 我想使用“值”类(应等于100)来计算所有td的总和。 I then want to divide the total of "value" (100) by the number or table rows (4) and then display the new toal (25) in the th class "total". 然后,我想将“值”(100)的总数除以数字或表行(4),然后在第“总计”类别中显示新的总计(25)。 I've tried several methods and can't figure this one out. 我尝试了几种方法,无法解决这一问题。 Any help would be deeply appreciated! 任何帮助将不胜感激!

You can query the cells containing your values using document.querySelectorAll and summing up each cell, calling parseFloat on each cell's innerHTML, then insert the average into the total cell using querySelectorAll again. 您可以使用document.querySelectorAll查询包含您的值的单元格,并对每个单元格求和,对每个单元格的innerHTML调用parseFloat ,然后再次使用querySelectorAll将平均值插入总单元格中。 No jQuery needed. 无需jQuery。

 var valueCells = document.querySelectorAll('.value'); var sum = 0; for (var i = 0; i < valueCells.length; ++i) { sum += parseFloat(valueCells[i].innerHTML); } document.querySelectorAll('.total')[0].innerHTML = (sum / valueCells.length).toFixed(2); 
 <table> <tr><th>My Table<th class="total"></th></tr> <tr><td class="title">title 1</td><td class="value">10%</td></tr> <tr><td class="title">title 1</td><td class="value">20%</td></tr> <tr><td class="title">title 1</td><td class="value">30%</td></tr> <tr><td class="title">title 1</td><td class="value">40%</td></tr> </table> 

You can try below code.. Hope it helps 您可以尝试下面的代码。希望对您有所帮助

 var total = 0; //loop through all td with class value $("td.value").each(function(index) { //remove the last character from the text and convert to float //then add it to variable total total += parseFloat($(this).text().slice(0,-1)); }); //divide total with (number of rows - 1) //-1 to remove the first row which is the heading total = total / ($("table tr").length - 1); //assign the result to th with class total $("th.total").text(total); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr><th>My Table><th class="total"></th></tr> <tr><td class="title">title 1</td><td class="value">10%</td></tr> <tr><td class="title">title 1</td><td class="value">20%</td></tr> <tr><td class="title">title 1</td><td class="value">30%</td></tr> <tr><td class="title">title 1</td><td class="value">40%</td></tr> </table> 

The sample way, to get desired output using : 示例方法,使用获得所需的输出:

you can have Answer with limited to 2 decimal places 您可以将答案限制在2个小数位以内

Demo here 在这里演示

 var total =0, count =0; //iterate through all tr without using index $('table .value').each(function() { //read inner html, add to a variable total += parseFloat($(this).html().slice(0,-1)); //get total number of '.value' count ++; }); //show avg. output in '.total' th $('table .total').html((total/count).toFixed(2)); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr><th>My Table</th><th class="total"></th></tr> <tr><td class="title">title 1</td><td class="value">10%</td></tr> <tr><td class="title">title 1</td><td class="value">20%</td></tr> <tr><td class="title">title 1</td><td class="value">30%</td></tr> <tr><td class="title">title 1</td><td class="value">40%</td></tr> </table> 

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

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