在学生标记列表中,我自动更改了标记1总数和平均值将发生变化 我在jquery
中使用了这样的代码$("#total_" + this.name)[0].innerHTML = parseFloat($("#marks1_" + this.name).val()) + parseFloat($("#marks2_" + this.name).val()) + parseFloat($("#marks3_" + this.name).val());
$("#average_" + this.name)[0].innerHTML = (((parseFloat($("#marks1_" + this.name).val()) + parseFloat($("#marks2_" + this.name).val()) + parseFloat($("#marks3_" + this.name).val())) / 300) * 100).toFixed(2);
像这样如何在不使用每个循环的情况下自动更改总计
我像这样使用了总计
$("#grandtotal")[0].innerHTML += parseFloat($("#marks1_" + this.name).val()) + parseFloat($("#marks2_" + this.name).val()) + parseFloat($("#marks3_" + this.name).val());
答案 0 :(得分:0)
在设置新学生总值之前,请获取旧值:
var oldStudentTotal = parseFloat($("#total_" + this.name).html());
var newStudentTotal = parseFloat($("#marks1_" + this.name).val()) +
parseFloat($("#marks2_" + this.name).val()) +
parseFloat($("#marks3_" + this.name).val());
$("#total_" + this.name).html(newStudentTotal);
然后,从旧总计中减去旧学生总值并添加新学生总值:
var oldGrandTotal = parseFloat($("#grandtotal").html());
var newGrandTotal = oldGrandTotal - oldStudentTotal + newStudentTotal;
$("#grandtotal").html(newGrandTotal);
对平均值做同样的事情。
答案 1 :(得分:0)
拯救的递归!
function rowRec($currentRow) {
if ($currentRow.length == 0)
return 0;
var rowTotal = cellRec($currentRow.find(".mark").first());
$currentRow.find(".total").text(rowTotal);
$currentRow.find(".average").text(rowTotal / $currentRow.find(".mark").length);
return rowTotal + rowRec($currentRow.next(".resultsRow"));
}
function cellRec($currentCell) {
if ($currentCell.length == 0)
return 0;
var thisCellValue = parseFloat($currentCell.text());
return thisCellValue + cellRec($currentCell.next(".mark"));
}
var grandTotal = rowRec($("tr.resultsRow").first());
var grandAvg = grandTotal / ($(".mark").length);
$(".grand_total").text(grandTotal);
$(".grand_average").text(grandAvg);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Mark1</th>
<th>Mark2</th>
<th>Mark3</th>
<th>Total</th>
<th>Average</th>
</tr>
</thead>
<tbody>
<tr class="resultsRow">
<td class="name">Name 1</td>
<td class="age">14</td>
<td class="mark">75</td>
<td class="mark">77</td>
<td class="mark">84</td>
<td class="total"></td>
<td class="average"></td>
</tr>
<tr class="resultsRow">
<td class="name">Name 2</td>
<td class="age">17</td>
<td class="mark">43</td>
<td class="mark">56</td>
<td class="mark">32</td>
<td class="total"></td>
<td class="average"></td>
</tr>
<tr class="resultsRow">
<td class="name">Name 3</td>
<td class="age">16</td>
<td class="mark">87</td>
<td class="mark">99</td>
<td class="mark">75</td>
<td class="total"></td>
<td class="average"></td>
</tr>
<tr class="resultsRow">
<td class="name">Name 4</td>
<td class="age">15</td>
<td class="mark">54</td>
<td class="mark">67</td>
<td class="mark">23</td>
<td class="total"></td>
<td class="average"></td>
</tr>
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td class="grand_total"></td>
<td class="grand_average"></td>
</tr>
</tfoot>
</table>
&#13;