gpt4 book ai didi

javascript - 为什么在JS中使用for循环遍历数组时页面报错?

转载 作者:行者123 更新时间:2023-12-04 08:39:17 25 4
gpt4 key购买 nike

今天,我尝试编写一个可以接收用户数据并将其显示给用户的网页。因此,我使用数组来存储数据并使用 for 循环来读取数据。
当我运行代码时,第 16 行出现错误(Uncaught TypeError: Cannot read property 'length' of undefined)。我想我以错误的方式使用了 for 循环。如果有人能弄清楚,请注意我。谢谢!

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
var students = [["2019001","like",89,92,91],["2019002","zhnagsan",67,89,77]];
function load_data()
{
var table_result;
table_result = "<table border=1>";
table_result += "<tr><th>student number</th><th>student name</th><th>score1</th><th>score2</th><th>score3</th></tr>";
for (var i = 0; i < students.length; i++)
{
table_result += "<tr>";
for (var j = 0; j < students[i].length; j++)
{
table_result += "<td>"+students[i][j]+"</td>"
}
table_result += "</tr>";
}
table_result += "</table>";
document.getElementById('result').innerHTML = table_result;
}
function add_new()
{
var studentName, studentNumber, score1, score2, score3;
studentName = document.getElementById("studentName").value;
studentNumber = document.getElementById("studentNumber").value;
score1 = document.getElementById("score1").value;
score2 = document.getElementById("score2").value;
score3 = document.getElementById("score3").value;
var addition;
additon = new Array(studentNumber,studentName,score1,score2,score3);
students.push(addition);
load_data();
}
</script>
</head>
<body onload="load_data()">
<div id="result">
</div>
<form>
student name: <input type="text" id="studentName"><br>
student number: <input type="text" id="studentNumber"><br>
score1: <input type="text" id="score1"><br>
score2: <input type="text" id="score2"><br>
score3: <input type="text" id="score3"><br>
<input type="button" id="button" onclick="add_new()" value="add"><br>
</form>
</body>
</html>

最佳答案

像这样尝试:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
var students = [["2019001","like",89,92,91],["2019002","zhnagsan",67,89,77]];
function load_data()
{
var table_result;
table_result = "<table border=1>";
table_result += "<tr><th>student number</th><th>student name</th><th>score1</th><th>score2</th><th>score3</th></tr>";
for (var i = 0; i < students.length; i++)
{
table_result += "<tr>";
for (var j = 0; j < students[i].length; j++)
{
table_result += "<td>"+students[i][j]+"</td>"
}
table_result += "</tr>";
}
table_result += "</table>";
document.getElementById('result').innerHTML = table_result;
}
function add_new()
{
var studentName, studentNumber, score1, score2, score3;
studentName = document.getElementById("studentName").value;
studentNumber = document.getElementById("studentNumber").value;
score1 = document.getElementById("score1").value;
score2 = document.getElementById("score2").value;
score3 = document.getElementById("score3").value;
var addition;
addition = new Array(studentNumber,studentName,score1,score2,score3);
students.push(addition);
load_data();
}
</script>
</head>
<body onload="load_data()">
<div id="result">
</div>
<form>
student name: <input type="text" id="studentName"><br>
student number: <input type="text" id="studentNumber"><br>
score1: <input type="text" id="score1"><br>
score2: <input type="text" id="score2"><br>
score3: <input type="text" id="score3"><br>
<input type="button" id="button" onclick="add_new()" value="add"><br>
</form>
</body>
</html>

唯一的问题是 addition 的拼写错误在这一行:
additon = new Array(studentNumber,studentName,score1,score2,score3);

关于javascript - 为什么在JS中使用for循环遍历数组时页面报错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64641943/

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