gpt4 book ai didi

javascript - 计算所选复选框分数的最大值和平均值

转载 作者:行者123 更新时间:2023-12-03 04:07:15 26 4
gpt4 key购买 nike

我需要下表中所选分数的最大值和平均值。我尝试了不同的方式。但我不能。请在这件事上给予我帮助。我也添加了 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>

最佳答案

当您选中该复选框时,您将获得平均值。

$(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);
}

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id='dynamicTable'></table>
<input type='button' id='cal' value='Calculate'>

有关最大检查的更多信息here

关于javascript - 计算所选复选框分数的最大值和平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44496313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com