简体   繁体   English

JavaScript测验不会回忆起以前的答案

[英]Javascript quiz won't recall previous answers

I'm new to programming and Javascript, and as a first project I'm trying to create a small quiz application. 我是编程和Java语言的新手,并且作为第一个项目,我试图创建一个小的测验应用程序。

I've included it here on jsfiddle: http://jsfiddle.net/ben220/L0y43q3d/ 我在jsfiddle上包括了它: http : //jsfiddle.net/ben220/L0y43q3d/

I've tried to design the program so that the user is able go back through the quiz and change any of their answers before they click to get their final score at the end. 我已经尝试过设计程序,以便用户可以单击测验并更改其答案,然后再单击以最终获得最终分数。 The user checks the radio button next to their chosen answer. 用户选中所选答案旁边的单选按钮。 When the user clicks 'next' to move on (or 'back' to revisit a previous question) their answer is passed to an array called 'choices', and this array will store all of the user's answers as they go through the quiz. 当用户单击“下一个”继续(或单击“上一步”以再次访问上一个问题)时,他们的答案将传递到名为“选择”的数组,并且该数组将存储用户通过测验时的所有答案。

I've managed to write the program so that previous answers can be saved and recalled but there is one annoying problem. 我设法编写了程序,以便可以保存和调出以前的答案,但是有一个烦人的问题。 If the user checks a particular radio button more than once in the quiz, the program will only remember the first one. 如果用户在测验中多次检查特定的单选按钮,则程序将仅记住第一个按钮。 For example: if I am on Question 4 and check the radio button for Answer 2, I might decide to go back to the previous question and change my answer, before moving on. 例如:如果我在问题4上,并选中答案2的单选按钮,我可能会决定继续前一个问题并更改答案,然后再继续。 So I go back to Question 3 and change the answer to Answer 2. I have now chosen the same radio button on two consecutive questions. 因此,我回到问题3并更改答案2的答案。我现在在两个连续的问题上选择了相同的单选按钮。 But now when I click 'next' to move onto Question 4 again I find that no radio button is checked, my answer was not saved. 但是现在,当我单击“下一步”再次移至问题4时,我发现未选中任何单选按钮,则未保存我的答案。

I've really tried to fix this by myself but I do not understand what I've done wrong. 我确实尝试自己解决此问题,但是我不明白自己做错了什么。 If anyone out there could suggest a solution I'd be grateful. 如果有人可以提出解决方案,我将不胜感激。 If any further clarification is needed let me know. 如果需要进一步澄清,请告诉我。 Sorry if my explanation or my code is confusing - I'm still a newbie with Javascript! 抱歉,如果我的解释或代码令人困惑-我仍然是Java语言的新手! Many thanks. 非常感谢。

<div id="container">
    <div id="quizArea">
        <h2 id="theQuestion">Question 1</h2>
        <fieldset id="myForm">
                <label><input type="radio" name="question" value="0" />Answer 1</label><br/>
                <label><input type="radio" name="question" value="1" />Answer 2</label><br/>
                <label><input type="radio" name="question" value="2" />Answer 3</label><br/>
                <label><input type="radio" name="question" value="3" />Answer 4</label><br/>
                <label><input type="radio" name="question" value="4" />Answer 5</label><br/>
        </fieldset>
        <div class="mini_container"><button id="next">Next</button></div>
        <button id="getScore">Get Score</button>
        <div class="mini_container"><button id="back">Back</button></div>
        <p id="submitError">Please choose an answer before proceeding to the next question</p>
        <div id="score"></div>
      </div>
 </div>

Javascript: 使用Javascript:

var module = (function () {

var theQuestions = [{question: "Question 1", choices: ["Answer 1", "Answer 2", "Answer 3", "Answer 4", "Answer 5"], answer: 2}, {question: "Question 2", choices: ["Answer 1", "Answer 2", "Answer 3", "Answer 4", "Answer 5"], answer: 4}, {question: "Question 3", choices: ["Answer 1", "Answer 2", "Answer 3", "Answer 4", "Answer 5"], answer: 1}, {question: "Question 4", choices: ["Answer 1", "Answer 2", "Answer 3", "Answer 4", "Answer 5"], answer: 1}, {question: "Question 5", choices: ["Answer 1", "Answer 2", "Answer 3", "Answer 4", "Answer 5"], answer: 3}],
score = 0,
title = document.getElementById("theQuestion"),
back = document.getElementById("back"),
next = document.getElementById("next"),
radioButtons = document.getElementById("options"),
options = document.getElementsByTagName("label"),
radios = document.getElementsByTagName("input"),
submitError = document.getElementById("submitError"),
scoreBtn = document.getElementById("getScore"),
scoreBox = document.getElementById("score"),
currentQ = 0;

var choices = [];

function getRadioInfo() {
var decision = null;
for(var i = 0; i < radios.length; i += 1) {
        if(radios[i].checked) {
            decision = radios[i].value;
        }
    }
return decision;
}

back.onclick = function() {
var decision = getRadioInfo();
choices[currentQ] = decision;
currentQ -= 1;
if(currentQ === 0) {
    back.style.display = "none";
}
if(currentQ < theQuestions.length) {
next.style.display = "block";
scoreBtn.style.display = "none";
}
title.innerHTML = theQuestions[currentQ].question;
var a = 0;
for(var i = 0; i < options.length; i += 1) {
    options[i].innerHTML = '<input type="radio" name="question" value="' + a + '" />' + theQuestions[currentQ].choices[a];
    a += 1;
}
restorePreviousAnswer();
};

var restorePreviousAnswer = function() {
for(var i = 0; i < choices.length; i += 1) {
    if(choices.indexOf(choices[i]) == currentQ) {
        var prev = choices[i];
    }
}
for(var x = 0; x < radios.length; x += 1) {
    if (radios[x].value == prev) {
        radios[x].checked = true;
    }
}
};


next.onclick = function() {
    var decision = getRadioInfo();
    if(decision == null) {
        submitError.style.display = "block";
        return;
    } else {
        submitError.style.display = "none";
    }
    choices[currentQ] = decision;
    currentQ += 1;
    if(currentQ > 0) {
        back.style.display = "block";
    }
    if(currentQ == theQuestions.length - 1) {
        next.style.display = "none";
        scoreBtn.style.display = "block";
    }
    title.innerHTML = theQuestions[currentQ].question;
    var a = 0;
    for(var i = 0; i < options.length; i += 1) {
        options[i].innerHTML = '<input type="radio" name="question" value="' + a + '" />' + theQuestions[currentQ].choices[a];
        a += 1;
    }
    restorePreviousAnswer();

};

scoreBtn.onclick = function() {
hideAll();
scoreBox.innerHTML = "You scored " + score + " out of " + theQuestions.length;
};

})();

I cleaned up your restorePreviousAnswer function to just if (choices[currentQ] != null) radios[choices[currentQ]].checked = true . 我清理了您的restorePreviousAnswer函数,使其变为if (choices[currentQ] != null) radios[choices[currentQ]].checked = true Another part of the problem was that null values were being inserted in your array of answers when you went back before selecting a radio button. 问题的另一部分是,当您选择单选按钮之前返回时,将空值插入到答案数组中。

jsFiddle example jsFiddle示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM