Javascript 当我使用for循环遍历JS中的数组时,为什么页面会报告错误?
今天,我尝试编写一个网页,可以接收用户的数据并将其显示给用户。因此,我使用数组来存储数据,并使用for循环来读取数据。 运行代码时,第16行出现错误(UncaughtTypeError:无法读取undefined的属性'length')。我猜我用错了for循环。如果有人能弄明白,请通知我。谢谢Javascript 当我使用for循环遍历JS中的数组时,为什么页面会报告错误?,javascript,html,Javascript,Html,今天,我尝试编写一个网页,可以接收用户的数据并将其显示给用户。因此,我使用数组来存储数据,并使用for循环来读取数据。 运行代码时,第16行出现错误(UncaughtTypeError:无法读取undefined的属性'length')。我猜我用错了for循环。如果有人能弄明白,请通知我。谢谢 <!DOCTYPE html> <html> <head> <meta charset="utf-8">
<!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>
var学生=[“2019001”,“like”,89,92,91],“2019002”,“zhnagsan”,67,89,77];
函数加载_数据()
{
var表结果;
表_结果=”;
表_结果+=“学生编号学生姓名Core1Core2Core3”;
for(var i=0;i
学号:
分数1:
得分2:
得分3:
试着这样做:
var学生=[“2019001”,“like”,89,92,91],“2019002”,“zhnagsan”,67,89,77];
函数加载_数据()
{
var表结果;
表_结果=”;
表_结果+=“学生编号学生姓名Core1Core2Core3”;
for(var i=0;i
学号:
分数1:
得分2:
得分3: