我需要下表中所选分数的最大值和平均值。以不同的方式进行了尝试。但我无法做到。请帮我解决一下这个。 我也添加了html。
$(document).ready(function(){
var employees = [];
function employee(id,name,score,mail,other)
{
this.id= id;
this.name=name;
this.score=score;
this.mail=mail;
this.other=other;
this.selected=false;
}
employees.push(new employee(1,'XYZ',114,'abcd@abc.com',""));
employees.push(new employee(2,'ABC',321,'abc@abc.com',""));
employees.push(new employee(3,'','','',''));
$.each(employees, function(index, employee){
var row = $("<tr id='tdata"+index+"'>");
var column ="";
column += "<td><label><input type='checkbox' id='selectBox"+index+"'
class='case'><span></span></label></td>";
column += "<td>"+employee.name +"</td>";
column += "<td id='score"+index+"' class='score'>"+ employee.score+"
</td>";
column += "<td>"+ employee.mail +"</td>";
column += "<td>"+ '' +"</td>";
row.append(column);
$("table#dynamicTable").append(row);
});
这是html代码。在此表中,每当选择某个复选框时,必须考虑相应的分数,并且必须写出它们的平均值,其中最大值必须写入。
<html>
<body>
<table id="dynamicTable" class="searchtbl">
<tr>
<th><label><input type="checkbox" id="selectall" /><span></span></label>
</th>
<th>Name</th>
<th>Score</th>
<th>Email</th>
<th></th>
</tr>
</table>
<input type="button" id="Calculate" value="Calculate"/>
<label>Average:</label>
<label class="avg-max" id="Average"></label>
<label>Max:</label>
<label class="avg-max" id="Max"></label>
</body>
</html>
答案 0 :(得分:1)
选中该复选框后,您将获得平均值。
Sleep
$(document).ready(function(){
var employees = [];
function employee(id,name,score,mail,other)
{
this.id= id;
this.name=name;
this.score=score;
this.mail=mail;
this.other=other;
this.selected=false;
}
employees.push(new employee(1,'XYZ',114,'abcd@abc.com',""));
employees.push(new employee(2,'ABC',321,'abc@abc.com',""));
employees.push(new employee(3,'','','',''));
$.each(employees, function(index, employee){
var row = $("<tr id='tdata"+index+"'>");
var column ="";
column += "<td><label><input type='checkbox' id='selectBox"+index+"' class='case'><span></span></label></td>";
column += "<td>"+employee.name +"</td>";
column += "<td id='score"+index+"' class='score'>"+ employee.score+"</td>";
column += "<td>"+ employee.mail +"</td>";
column += "<td>"+ '' +"</td>";
row.append(column);
$("table#dynamicTable").append(row);
});
$('#cal').on('click',function(){
max_avg();
});
function max_avg(){
var score =[];
$('input[type=checkbox]:checked').each(function () {
score.push($(this).closest('tr').find('td.score').text());
});
var sum = 0;
for( var i = 0; i < score.length; i++ ){
if(!isNaN(parseInt( score[i])))
{
sum += parseInt( score[i]);
}
}
var avg = sum/score.length;
var max = Math.max(...score);
console.log(avg+'-'+max);
}
});
有关最大支票here
的详细信息