gpt4 book ai didi

javascript - 计算用户使用 OnClick() 放入 HTML 表中的数字的平均值

转载 作者:可可西里 更新时间:2023-11-01 12:59:28 25 4
gpt4 key购买 nike

目前在识别我的 HTML/JavaScript 代码中的错误时遇到问题,该错误阻止我的 JavaScript 函数计算表格用户输入的五个数字的平均值。

当我在所需的单元格中输入数字后单击“计算成绩”时,没有任何反应。努力看到问题。

var table = document.getElementById("myTable");
var rows = table.rows;
for (var i = 0; i < rows.length; i++) {
var cells = rows[i].cells;
var sum = 0;
for (var x = 0; x < cells.length; x++) {
var cell = cells[x];
sum += parseInt(cell.innerHTML);
}
var average = sum / cells.length;
rows[i].innerHTML += "<td>" + average + "</td>";
}
<table id="myTable" style="width:100%">
<tr>
<th id="StudentName" bgcolor="FFF55A">Name</th>
<th id="IDnumber" bgcolor="#FFF55A">ID</th>
<th bgcolor="#FFF55A">Assignment 1</th>
<th bgcolor="#FFF55A">Assignment 2</th>
<th bgcolor="#FFF55A">Assignment 3</th>
<th bgcolor="#FFF55A">Assignment 4</th>
<th bgcolor="#FFF55A">Assignment 5</th>
<th bgcolor="#FFF55A">Final</th>
</tr>
<tr>
<td contenteditable='false'>Person1 </td>
<td>1546396</td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
</tr>
<tr>
<td contenteditable='false'>Person2</td>
<td contenteditable='false'>45942544</td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
</tr>
<tr>
<td contenteditable='false'>Person3</td>
<td contenteditable='false'>48498465</td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
</tr>
<tr>
<td contenteditable='false'> Person4</td>
<td contenteditable='false'>4964984</td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
</tr>
<tr>
<td contenteditable='false'>Person5</td>
<td contenteditable='false'> 5686846</td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
</tr>
<tr>
<td contenteditable='false'>Person6</td>
<td contenteditable='false'>498465</td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
</tr>
<tr>
<td contenteditable='false'>Person7</td>
<td contenteditable='false'>654684</td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
</tr>
<tr>
<td contenteditable='false'>Person8</td>
<td contenteditable='false'>6498497</td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
</tr>
<tr>
<td contenteditable='false'>Person9</td>
<td contenteditable='false'>8749846</td>
<td contenteditable='true'>2</td>
<td contenteditable='true'>45</td>
<td contenteditable='true'>57</td>
<td contenteditable='true'>2</td>
<td contenteditable='true'>4</td>
</tr>
<tr>
<td contenteditable='false'>Person10</td>
<td contenteditable='false'>984894</td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
</tr>
</table>
<button onclick="myFunction()" class="button1">Calculate grade</button>

最佳答案

我稍微编辑了一下你的 fiddle :https://jsfiddle.net/StarStep/m7v1j5zz/105/并在计算按钮上添加了一个事件监听器: <button id="myFunction" class="button1">Calculate grade</button>

function myFunction() {
var table = document.getElementById("myTable");
var rows = table.rows;

for (var i = 1; i < rows.length; i++) {
var cells = rows[i].cells;
var sum = 0;
var numbers = 0;
for (var x = 2; x < (cells.length -1); x++) {
var cell = cells[x];
var addme = parseInt(cell.innerHTML);
if(!isNaN(addme)) {
sum += addme;
numbers++;
}
}
var average = sum / numbers;
rows[i].cells[7].innerHTML = "<td>" + Math.round(average) + "</td>";
}
}

document.getElementById('myFunction').onclick = myFunction;

关于javascript - 计算用户使用 OnClick() 放入 HTML 表中的数字的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42448503/

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