使用keyup事件计算javascript中的总平均值

我的应用程序中使用laravel来计算总平均值时缺少函数。 因为它需要javascript,我遇到了麻烦,现在使用foreach简化了这个静态值,但由于我有平均脚本,现在我需要一个总平均值的函数。

希望所有人都可以帮助我完成我的工作。 先感谢您。

 $("input").on("keyup", function() { $("tbody tr").each(function() { var col = 1; var tr = 1; var t = 0; var a = 0; $(this).children('td').not(':first').not(':last').each(function() { var number = ($(this).children('input').length == 0) ? $(this).html() : $(this).children('input').first().val(); t += parseInt(number); a = t / col; col++; }); $(this).children('td').last().html(a); col = 1; tr++; }); //getTotalave(); }); 
    
Grade score 1 score 2 score 3 score 4 average
student1 84
student2 84.25
student3 87.25
student4 86
Total Average 85.375

使用唯一的类名称:

 $("input").on("keyup", function() { var tAv = 0, tRo = 0; $("tbody tr").not(':last').each(function() { var col = 0, t = 0; // starting counters tRo++; $(this).children('td').not(':first').not(':last').each(function() { t += parseFloat($('input', this).val()) || 0; col++; }); var rAv = t / col; // row average tAv += rAv; // increment average $('td', this).last().text(rAv.toFixed(2)); // row average display }); $('.tave').text((tAv / tRo).toFixed(2)); // final average }); 
    
Grade score 1 score 2 score 3 score 4 average
student1 84
student2 84.25
student3 87.25
student4 86
Total Average 85.375