我有一个我找不到解决方案的问题。在我的代码中有一个名为list
. 该变量list
应该使用 检索输入元素,但在语句结束后getElementsByName()
该变量为空(即list.length == 0
) 。if
在下面的代码中,在for
循环的开头list.length
是零或未定义。
为什么会这样?
功能是:
function chekRadioToAnswer() {
var signAnswers = new Array(0, 0, 0, 0);
var grades = [[0, 5, 10, 15], [15, 12, 10, 5, 0], [0, 5, 10, 15, 20], [0, 0, 20, 30]];
var answers = new Array(0, 0, 0, 0, 0, 0);
for (num = 2; num <= 5; num++) {
if (num == 2) {
list = document.getElementsByName("answer2");
}
if (num == 3) {
list = document.getElementsByName("answer3");
}
if (num == 4) {
list = document.getElementsByName("answer4");
}
if (num == 5) {
list = document.getElementsByName("answer5");
}
for (i = 0; i < list.length; i++) {
if (list[i].checked) {
var value = document.getElementById("hiddenAnswers").getAttribute("value");
document.getElementById("hiddenAnswers").setAttribute("value", (value + grades[num - 2][i]));
break;
}
}
list.length = 0;
}
return false;
}