JavaScript循环在一个简单的测验中

时间:2012-11-21 09:25:38

标签: javascript arrays loops firebug

基于JavaScript的简单学校测验项目,这些是整个测验包含的文件:

它会加载一个带有相应答案的问题,因此工作正常。 (参见上面的输出)。现在,只要选择或不选择答案,单击“下一个问题”,就会发生两种不同的事情。

未选择答案: http://www.rapidimg.org/server/files/50ace9123bce6TzoiqL.png - 加载问题2 +答案,但将页面置于无限加载状态。当我点击另一个答案并点击“下一个问题”时,Firebug显示:http://www.rapidimg.org/server/files/50ace9a08b88aqLFZ1K.png

选择了一个答案: http://www.rapidimg.org/server/files/50acea1422aed6gZkcL.png - 加载问题3 +答案(???)但也会使页面处于无限负载状态。单击答案+“下一个问题”按钮时出现与上述相同的错误:http://www.rapidimg.org/server/files/50acea4e4bc3cLn8g2U.png

以下是“quiz_functions.js”的代码:

var useranswers = new Array();
var imgArray = new Array();
var answered = 0;
var currentQuestion = 0;


function renderQuizViaArray() 
{
    document.write('<h1>' + questions[currentQuestion] + '</h1>');

    for(i=0, j=choices[currentQuestion].length; i < j; i++)
    {
        document.writeln('<input type="radio" name="answer_' + currentQuestion + '" value="1" id="answer_' + currentQuestion + '_' + i + '" class="question_' + currentQuestion + '" onclick="submitAnswer(' + currentQuestion + ', this, \'question_' + currentQuestion + '\', \'label_' + currentQuestion + '_' + i + '\')" /><label id="label_' + currentQuestion + '_' + i + '" for="answer_' + currentQuestion + '_' + i + '"> ' + choices[currentQuestion][i] + '</label><br />');
    }

        document.write('<input type="submit" value="Next Question" onclick="nextQuestion()" />');
}

function nextQuestion()
{
    currentQuestion++;
    renderQuizViaArray();
}

function submitAnswer(questionId, obj, classId, labelId) 
{
  useranswers[questionId] = obj.value;
  document.getElementById(labelId).style.fontWeight = "bold";
  disableQuestion(classId);
  answered++;
}

function disableQuestion(classId) 
{
  var alltags = document.all? document.all : document.getElementsByTagName("*")
  for (i = 0; i < alltags.length; i++) 
  {
    if (alltags[i].className == classId) 
    {
      alltags[i].disabled = true;
    }
  }
}

0 个答案:

没有答案