我想要一点帮助,
我的下表是:
<table class="tableTest" id="tblOne">
<thead>
<tr><strong><h1>Players</h1></strong></tr>
<tr>
<th>Player name</th>
<th># hits</th>
<th>AVG</th>
</tr>
</thead>
<tbody>
<tr>
<td>Charles Xaviers</td>
<td class="playerClass">10</td>
<td class="test"> </td>
</tr>
<tr>
<td>Gon Freecs</td>
<td class="playerClass">17</td>
<td class="test"> </td>
</tr>
<tr>
<td>J.K Rolling</td>
<td class="playerClass">21</td>
<td class="test"> </td>
</tr>
<tr>
<td><b>Total</b></td>
<td class="totalCountPlayer"> </td>
<td>100%</td>
</tr>
<tr>
<td> </td>
<td><b>AVG </b></td>
<td class="avgPlayers"> </td>
</tr>
</tbody>
这是我计算的jquery代码
$(document).ready(function() {
var classSum = 'playerClass'; var classTotal = 'totalCountPlayer'; var ClassAvg = 'avgPlayers';
calculate(classSum, classTotal, ClassAvg);
function calculate(classSum, classTotal, ClassAvg) {
var sumatoria = $('.' + classSum);
var total = 0;
var count = -1;
$.each(sumatoria, function(i, suma){
var pc=$(this).text();
if (pc!= 'NA'){
total = total + parseInt(pc,10);
count++;
avg(pc);
}});
$('.' + classTotal).text(total);
$('.' + ClassAvg).text((total / count).toFixed(3));
function avg(pc){
var avg = 0;
avg = (pc/total);
$( "td.test" ).text(avg);
}
}
});
我试图计算每个项目的平均值,但总是采取最后的平均值计算我该如何在.test类中编写计算,请帮帮我
应该是这样的:
<table class="tableTest" id="tblOne">
<tr><strong><h1>Players</h1></strong></tr>
<tr>
<th>Player name</th>
<th># hits</th>
<th>AVG</th>
</tr>
<tr>
<td>Charles Xaviers</td>
<td class="playerClass">10</td>
<td class="test">0.20%</td>
</tr>
<tr>
<td>Gon Freecs</td>
<td class="playerClass">17</td>
<td class="test">0.35%</td>
</tr>
<tr>
<td>J.K Rolling</td>
<td class="playerClass">21</td>
<td class="test">0.43%</td>
</tr>
<tr>
<td><b>Total</b></td>
<td class="totalCountPlayer"> </td>
<td>100%</td>
</tr>
<tr>
<td> </td>
<td><b>AVG </b></td>
<td class="avgPlayers"> </td>
</tr>
答案 0 :(得分:1)
你的两个主要问题是:
td
,但$( "td.test" )
每td
个test
类定位total
未正确计算(一旦#1得到解决,您会看到)
尝试以下操作,这有点&#34;快速而且肮脏&#34;:
$(document).ready(function() {
var classSum = 'playerClass'; var classTotal = 'totalCountPlayer'; var ClassAvg = 'avgPlayers';
var grandTotal = getGrandTotal();
calculate(classSum, classTotal, ClassAvg);
function calculate(classSum, classTotal, ClassAvg) {
var sumatoria = $('.' + classSum);
var total = 0;
var count = 0; // formerly: -1
$.each(sumatoria, function(i, suma){
var pc=$(this).text();
if (pc!= 'NA'){
count++;
avg(pc, $(this).next());
}
});
$('.' + classTotal).text(grandTotal);
$('.' + ClassAvg).text((grandTotal / count).toFixed(3));
function avg(pc, $td){
var avg = 0;
avg = (pc/grandTotal);
$td.text(Math.round(avg * 100, 0) + '%');
}
}
function getGrandTotal() {
var sumatoria = $('.' + classSum);
var total = 0;
$.each(sumatoria, function(i, suma){
var pc=$(this).text();
if (pc!= 'NA'){
total = total + parseInt(pc, 10);
}
});
return total;
}
});
输出:
Player name # hits AVG
----------- ------ ---
Charles Xaviers 10 21%
Gon Freecs 17 35%
J.K Rolling 21 44%
Total 48 100%
AVG 16.000
注意:
您的第一个百分比(例如)实际上是20%(四舍五入时为21%),而不是.20%。所以,我做了那个修改,这意味着你的百分比现在总共100%,正如预期的那样。
正确的&#34; AVG&#34;将是16(48/3),而不是24.我也进行了修改,但如果由于某种原因你想保持24,只需将var count = 0;
改回var count = -1;
。