具体查询关于我的javascript测验

时间:2013-07-03 18:55:32

标签: javascript

下午好。我的测验问题非常具体。我真的需要帮助一件事。如果你正确回答问题,然后回到它,然后点击下一步,它会再次增加你的总分!如果你愿意,你可以得到无限分。我很难理解如何解决问题。我是个业余爱好者。请花点时间帮忙。上次关闭时我没有得到任何指导。

<!DOCTYPE HTML>
<html>

<head>
    <title>Aldine Assessment</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="style.css">


<script type = "text/javascript">
    var quiz = [
            {
                "question"      :   "&nbsp Q1: What is the name of this school district?",
                "choices"       :   [   "&nbsp This is Aldine Independent School District.",
                                        "&nbsp This is Spring Independent School District.",
                                        "&nbsp This is Klein Independent School District.",
                                        "&nbsp This is Houston Independent School District."],
                "correct"       :   "&nbsp This is Aldine Independent School District.", },
            {
                "question"      :   "&nbsp Q2: What city are we currently located in ?",
                "choices"       :   [   "&nbsp We are currently located in the city of Spring.",
                                        "&nbsp We are currently located in the city of Houston.",
                                        "&nbsp We are currently located in the city of Dallas.",
                                        "&nbsp We are currently located in the city of San Antonio." ],
                "correct"       :   "&nbsp We are currently located in the city of Houston.",},
            {
                "question"      :   "&nbsp Q3:What state are we currently located in?",
                "choices"       :   [   "&nbsp We are currently located in the state of Texas.",
                                        "&nbsp We are currently located in the state of Louisiana.",
                                        "&nbsp We are currently located in the state of Atlanta.",
                                        "&nbsp We are currently located in the state of Florida."],
                "correct"       :   "&nbsp We are currently located in the state of Texas.",},
            {   
                "question"      :   "&nbsp Q4:What Department are we currently in?",
                "choices"       :   [
                                        "&nbsp We are part of the Financial Services Department.",
                                        "&nbsp We are part of the Child Nutrition Department",
                                        "&nbsp We are part of the Technology Services Department",
                                        "&nbsp We are part of the Child Development Department"],

                "correct"       :   "&nbsp We are part of the Technology Services Department",},
            {   
                "question"      :   "&nbsp Q5:What month are we currently in?",
                "choices"       :   [
                                        "&nbsp We are currently in the month of June",
                                        "&nbsp We are currently in the month of July",
                                        "&nbsp We are currently in the month of August",
                                        "&nbsp We are currently in the month of September"
                                    ],
                "correct"       :   "&nbsp We are currently in the month of June",},
 ];

        var currentQuestion = 0;
        var score = 0;
        var askingQuestion = true;
        var radioAnswer= new Array();



        function loadQuestion(){
            //---------------set temporary variable for creating radio buttons-------------------------
            var radioButton;
            //---------------clear out radio buttons from previous question----------------------------
            document.getElementById('content').innerHTML = "";
            //---------------loop through choices, and create radio buttons----------------------------
            for(var i = 0; i < quiz[currentQuestion]["choices"].length; i++){
                radioButton  = document.createElement('input');
                radioButton.type = 'radio';
                radioButton.name = 'quiz'+currentQuestion;
                radioButton.id = 'choice'+ (i+1);
                if (radioAnswer[currentQuestion] == i)
                    {
                    radioButton.checked = true; 
                    }
                radioButton.value = quiz[currentQuestion]["choices"][i];
          //-------------------create label tag, which hold the actual text of the choices--------------
                var label = document.createElement('label');
                label.setAttribute('for','choice'+ (i+1));
                label.innerHTML = quiz[currentQuestion]["choices"][i];
          //-------------------------create a <br> tag to separate options-------------------------------
                var br = document.createElement('br');
                //attach them to content. Attach br tag, then label, then radio button

                document.getElementById('content').insertBefore(br, null);
                document.getElementById('content').insertBefore(label, br);
                document.getElementById('content').insertBefore(radioButton, label);
            }
        //----------------------------------load the question--------------------------------------------
            document.getElementById('question').innerHTML = quiz[currentQuestion]["question"];
        //---------------------------setup score for first time------------------------------------------
            if(currentQuestion == 0){
                document.getElementById('score').innerHTML = '<p>Score: 0 right answers out of ' + quiz.length +' possible</p>';
            }
        }

        function previous(){
        if(currentQuestion < quiz.length - 1){
                    currentQuestion--;
                    loadQuestion();
                } else {
                    showFinalResults();
                }
        }

        function checkAnswer(){

        //------------------------are we asking a question, or proceeding to next question?------------------
            if(askingQuestion){
        //---change button text to next question, so next time they click it, it goes to next question-------
                document.getElementById('check').innerHTML = 'Next Question';
                askingQuestion = false;
        //-------------------------determine which radio button they clicked---------------------------------
                var userpick;
                var correctIndex;
                var radios = document.getElementsByName('quiz'+currentQuestion);
                for(var i=0; i < radios.length; i++){
                    if(radios[i].checked){ //if this radio button is checked
                        userpick = radios[i].value;
                        radioAnswer[currentQuestion]= i;
                    }
        //-----------------------------------get index of correct answer-------------------------------------
                    if(radios[i].value == quiz[currentQuestion]["correct"]){
                        correctIndex = i;
                    }
                }
        //----------------------------set the color if they got it right, or wrong----------------------------
                if(userpick == quiz[currentQuestion]["correct"]){
                    score++;
                    document.getElementsByTagName('label')[correctIndex].style.color = "green";
                    document.getElementsByTagName('label')[correctIndex].style.fontWeight = "bold";
                } else {
                    document.getElementsByTagName('label')[correctIndex].style.color = "red";
                    document.getElementsByTagName('label')[correctIndex].style.fontWeight = "bold"; 
                }
                document.getElementById('score').innerHTML = '<p>Score: '+ score +' right answers out of ' + quiz.length +' possible</p>';
                } else { //reset form and move to next question
        //---------------------------------setting up so user can ask a question--------------------------------
                askingQuestion = true;
        //-------------------------------change button text back to 'submit answer'-----------------------------
                document.getElementById('check').innerHTML = 'Submit Answer';
        //-----------------------if we're not on last question, increase question number-------------------------
                if(currentQuestion < quiz.length - 1){
                    currentQuestion++;
                    loadQuestion();
                } else {
                    showFinalResults();
                }
            }
        }

        function showFinalResults(){

            document.getElementById('content').innerHTML = '<h2>You Completed The Quiz</h2>';
            document.getElementById('content').innerHTML += '<p>Below are your results:</p>';
            document.getElementById('content').innerHTML += '<h2>' + score + ' out of ' + quiz.length + ' questions, ' + Math.round(score/quiz.length * 100) + '%<h2>';
            //-------------------------------delete the button------------------------------------------------------
            var button = document.getElementById('check');
            button.parentNode.removeChild(button); //js requires you to delete elements from the parent
            //--------------------------------remove question-------------------------------------------------------
            document.getElementById('question').innerHTML = "";
        }
        window.onload = loadQuestion;
    </script>

</head>
<body>
<div id = "box" class = "unhidden">

    <h2>This Is A Quiz Provided By Aldine ISD</h2>
    <h4> There will be no points awarded for unanswered questions.</h4>


    <div style="position:absolute; left:680px; top:150px">
        <img src="../AldineLogo1.png" alt="Picture Cannot Be Viewed">
    </div>

    <div style="position:absolute; left:690px; top:350px"><button id="check" class="submit"  onclick="checkAnswer()">Submit Answer</button > </div>
    <div style="position:absolute; left:690px; top:405px"><button id="check2" class="previous"  onclick="previous()">Previous Question</button > </div>
  <div id="frame">
      <h3 id="question">Question here</h3>
            <div id="content" ></div>
            <div id="response"></div> 
  </div>
  <div id="score"><h3>Score: 0 right answers out of 0 possible</h3></div>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

answer字段添加到您的quiz结构中,以便在所有记录中questionchoicesanswer,并将其设置为 - 最初是1。然后,当用户选择答案时,将其值存储在此字段中。之后,在显示表单时检查answer值并禁用它,如果当前问题已经回答。