0

编写了一个函数,用于循环浏览在线测验中的问题。我在除 IE 之外的所有浏览器中都能正常工作(天哪,我希望 IE 会卷曲而死)。功能如下。

function cycleQs() {
var qs = document.getElementsByName("quizQ");
var nextQBtn = document.getElementById("btnNextQ");
var i = 0;
var curQ = -1;
for (i = 0; i < qs.length; i++) {
    if (qs[i].style.display == "block") {
        curQ = i;
    }
    //qs[i].style.display = "none";
}
var valid = false;
if (curQ > -1) {
    var qId = qs[curQ].id.replace("dv", "");
    var inps = document.getElementsByName(qId);
    if (inps.length > 0) {
        if (inps[0].type == "radio") {
            for (i = 0; i < inps.length; i++) {
                if (inps[i].checked) {
                    valid = true;
                }
            }
        } else if (inps[0].type == "hidden") {
            valid = true;
            for (i = 0; i < inps.length; i++) {
                if (inps[i].value <= 0) {
                    valid = false;
                }
            }
        }
    } else {
        valid = true;
    }
} else {
    valid = true;
}

if (valid == true) {
    for (i = 0; i < qs.length; i++) {
        qs[i].style.display = "none";
    }
    if (curQ < (qs.length - 1)) {
        qs[curQ + 1].style.display = "block";
        if (curQ == (qs.length - 2)) {
            var scoreDv = document.getElementById("dvScore");
            nextQBtn.style.display = "none";
            var answers = getAnswers();
            //alert(answers)
            scoreDv.innerHTML = "Processing";
            processResults(answers);
        }
    } else {
        qs[0].style.display = "block";    // Problem occurs here when function first loads
    }
} else {
    alert("You must select an answer before you can proceed");
}
}

有什么想法可以解决这个问题吗?

4

1 回答 1

0

document.getElementsByName由于 IE 的实施,可能不会返回任何内容:请参见此处

所以qs[0]可能不存在

于 2012-08-30T14:59:52.073 回答