function add() {
var students = [{
name: document.getElementById('nam').value,
id: document.getElementById('ied').value,
grade: document.getElementById('gr').value,
}]
document.ARR.Name.value = " ";
document.getElementById("gr").value = " ";
document.getElementById("ied").value = " ";
i = i + 1;
if (i == 5) {
alert("Max capacity reached")
}
}

<html>
<body>
Name: <input type="text" name="Name" id="nam" /></br>
</br>
Grade:<input type="text" name "Mark" id="gr" /> </br>
</br>
ID: <input type="text" name "ID" id="ied" /> </br>
</br>
<input type="button" value="Add" onclick="add()" />
</body>
</html>
&#13;
大家好,我试图从表单中获取信息并将它们放在一个对象数组中。这段代码不起作用
答案 0 :(得分:0)
我认为您尝试使用document.ARR.Name
作为对第一个输入字段的引用,但事实并非如此。只需通过getElementById
选择它。
function add() {
var students = [{
name: document.getElementById('nam').value,
id: document.getElementById('ied').value,
grade: document.getElementById('gr').value,
}];
document.getElementById('nam').value = " ";
document.getElementById("gr").value = " ";
document.getElementById("ied").value = " ";
/**i = i + 1;
if (i == 5) {
alert("Max capacity reached")
}*/ // I'm not sure what this is supposed to do, so I simply wrapped it in a comment, since it throws an error
}
&#13;
<html>
<body>
Name: <input type="text" name="Name" id="nam" /></br>
</br>
Grade:<input type="text" name "Mark" id="gr" /> </br>
</br>
ID: <input type="text" name "ID" id="ied" /> </br>
</br>
<input type="button" value="Add" onclick="add()" />
</body>
</html>
&#13;