请注意,我有一个脚本,可以自动将值输入到表格单元格中,它会根据普通html页面中的总值给出备注和等级。我如何自动加总从ajax返回字符串获得的相同值。下面是ajax返回字符串
//Ajax return strings
$.each(result, function(key, value)
{
string += "<tr class='rowca'><td>#</td><td>" +value.sID+ "</td><td>"
+value['Full Names']+ "</td><td><input type='text' name='ca1[]' id='ca1'
class='entry' maxlength='2' value='" +value.ca1+ "'></td><td><input
type='text' name='ca2[]' id='ca2' class='entry' maxlength='2' value='"
+value.ca2+ "'></td><td><input type='text' name='ca3[]' id='ca3'
class='entry' maxlength='2' value='" +value.ca3+ "'></td><td><input
type='text' name='ca4[]' id='ca4' class='entry' maxlength='2' value='"
+value.exam+ "'></td><td><input type='text' name='ca5[]' id='ca5'
class='total' disabled value='" +value.total+ "'></td><td><input type='text'
name='ca6[]' id='ca6' class='demo' disabled value='" +value.grade+ "'></td>
<td><input type='text' name='ca7[]' id='ca7' class='remark' disabled
value='" +value.remarks+ "'></td></tr><input type='text' id='sid[]' value='"
+value.sID+ "'>";
});
//Below is the script that works on the plain html page
$(document).ready(function(){
"use strict";
var rows = document.getElementsByClassName('rowca');
[].forEach.call(rows, function(rowca){
rowca.addEventListener('keyup', function()
{
var demos = rowca.getElementsByClassName('demo')[0];
var gtotal = rowca.getElementsByClassName('total');
var entries = rowca.getElementsByClassName('entry');
var remarks = rowca.getElementsByClassName('remark')[0];
var score = [].reduce.call(entries, function(sum, entry)
{
return +entry.value + sum;
}, 0);
gtotal.value = score || 0;
demos.value = getGrade(score);
remarks.value = getRemark(score);
});
});
function getGrade(score)
{
if (score > 90) { return 'A+'; }
else if (score > 80) { return 'A'; }
else if (score > 70) { return 'B+'; }
else if (score > 60) { return 'B'; }
else if (score > 50) { return 'C'; }
else if (score > 40) { return 'D'; }
else if (score > 30) { return 'E'; }
else if (score >= 0) { return 'F'; }
else { return 'No grade yet'; }
}
function getRemark(score)
{
if (score > 90) { return 'Excellent'; }
else if (score > 80) { return 'Distinction'; }
else if (score > 70) { return 'Very Good'; }
else if (score > 60) { return 'Good'; }
else if (score > 50) { return 'Credit'; }
else if (score > 40) { return 'Pass'; }
else if (score > 30) { return 'Poor'; }
else if (score >= 0) { return 'Fail'; }
else { return 'No grade yet'; }
}
});