使用正确的方式进行 JQuery 测试

4
我使用CodePen创建了一个jQuery测试。代码是有效的,但结果与我想要的不同。我想找一种方法同时显示问题和选项,并用绿色突出显示正确的答案和用红色突出显示错误的答案。以下是我尝试的代码:当用户的回答匹配正确答案时,将div突出显示为绿色,否则为红色。您能让我知道哪里错了吗? test.js
var quiz = [{
    question: "How can you get the type of arguments passed to a function?",
    answers: ["using typeof operator", "using getType function", "Both of the above", "None"],
    correctAnswer: 0
}, {
    question: "Which built-in method returns the character at the specified index?",
    answers: ["characterAt()", "getCharAt()", "charAt()", "None"],
    correctAnswer: 2
}]

var i = 0;
var score = 0;

$(document).ready(function () {
    $('#start').on('click', function () {
        $('#questions').text(quiz[i].question);
        $('#zero').text(quiz[i].answers[0]);
        $('#one').text(quiz[i].answers[1]); 

    });
});

$(document).ready(function () {
    $(document).on('click', '#next', function () {
        var answer = $('input[name="answers"]:checked').val();
        var answerString = quiz[i].answers[answer];
        $('p[class="userAnswer"][value=' + i + ']').text(answerString);
        var correctAnswer = quiz[i].correctAnswer;
        $('p[class="correctAnswer"][value=' + i + ']').text(quiz[i].answers[correctAnswer]);
        if (answer == quiz[i].correctAnswer) {

        } else {
            $('tr[class="row"][name=' + i + ']').css('background', '#FE2E64');
        }


        if (i < 2) {
            $('.choices').css('display', 'none');
            $('#questions').text(quiz[i].question);
            $('#zero').text(quiz[i].answers[0]);
            $('#one').text(quiz[i].answers[1]);
            $('.choices').show('slow');
            $('input[name="answers"]').prop('checked', false);

        }    

    });
});

    
     

你能否创建演示代码? - Swati
以上的代码是演示用的。 - User35
@Swathi https://jsfiddle.net/anjiroy/xrcsbv6z/1/ - User35
2个回答

1
要显示所有带有正确或错误答案的内容,您需要遍历所有问题,然后查看用户选择的答案和正确答案是否相同,根据此将所需的HTML附加到某个变量中。此外,每当用户在下面的代码段中选择任何答案时,我都会将其存储在某个数组中,以便在检查正确或错误答案时使用。
演示代码:

var correctAnswer;
var display_result = "";
var quiz = [{
  question: "How can you get the type of arguments passed to a function?",
  answers: ["using typeof operator", "using getType function", "Both of the above", "None"],
  correctAnswer: 0
}, {
  question: "Which built-in method returns the character at the specified index?",
  answers: ["characterAt()", "getCharAt()", "charAt()", "None"],
  correctAnswer: 2
}]

var i = 0;
var score = 0;

$(document).ready(function() {
  $('#start').on('click', function() {
    $('#questions').text(quiz[i].question);
    $('#zero').text(quiz[i].answers[0]);
    $('#one').text(quiz[i].answers[1]);
    $('#two').text(quiz[i].answers[2]);
    $('#three').text(quiz[i].answers[3]);
    $('#start').hide();
    $('.choices').show('slow');
    $('#next').show('slow');
  });
});
var users_answers = [];
$(document).ready(function() {
  $(document).on('click', '#next', function() {
    var answer = $('input[name="answers"]:checked').val();
    var answerString = quiz[i].answers[answer];
    $('p[class="userAnswer"][value=' + i + ']').text(answerString);
    correctAnswer = quiz[i].correctAnswer;
    $('p[class="correctAnswer"][value=' + i + ']').text(quiz[i].answers[correctAnswer]);
    users_answers.push(answer);
    if (answer == quiz[i].correctAnswer) {
      score++;
    } else {
      $('tr[class="row"][name=' + i + ']').css('background', '#FE2E64');
    }
    if (!$('input[name="answers"]').is(':checked')) {
      alert("please make a choice");
      return undefined; //stops executing the rest of the code
    }
    i++;

    if (i < 2) {
      console.log("in")
      $('.choices').css('display', 'none');
      $('#questions').text(quiz[i].question);
      $('#zero').text(quiz[i].answers[0]);
      $('#one').text(quiz[i].answers[1]);
      $('#two').text(quiz[i].answers[2]);
      $('#three').text(quiz[i].answers[3]);
      $('.choices').show('slow');
      $('input[name="answers"]').prop('checked', false);

    } else {
      //results
         $('#quiz').css('display', 'none');
            $('#start').css('display', 'block');
      var j = 0;
      //looping through quiz array
      $.each(quiz, function(index, val) {
      //getting correct answer
        var correctAnswer = val.correctAnswer;
        //appending question to variable
        display_result += " <div class='qtitle'>" + val.question + "</div>";
        //looping through answers array
        $.each(val.answers, function(index, val1) {
        //if answer is right
          if (users_answers[j] == correctAnswer && correctAnswer == index) {
          //append 
            display_result += "<b>You choose :</b> <div style='color:green'>" + val1 + "</div>"
          } else if (users_answers[j] == index) {
          //if answer is wrong 
            display_result += "<b>You choose :</b> <div style='color:red'>" + val1 + "</div>"
          } else if(correctAnswer== index) {
          //correct ans
            display_result += "<b>Correct Answer Was </b><div>" + val1 + "</div>"
          }else {
          //other answers
          display_result += "<div>" + val1 + "</div>"
          }

        });

        j++;//increment
      })
      //append score
      display_result +="<div id='score'>Total Score : "+ score+"</div>"
      $("#result").html(display_result);



    }
  });
});
#quiz {
  position: relative;
  height: 400px;
  width: 400px;
}

#start {
  position: absolute;
  height: 40px;
  width: 70px;
  bottom: 10%;
  right: 0%;
  background: #0080FF;
  color: #ffffff;
  padding: 6px;
  text-align: center;
  font-size: 150%;
  border-radius: 10px;
}

#start:hover {
  cursor: pointer;
  background: navy;
}

#next {
  position: absolute;
  height: 40px;
  width: 60px;
  bottom: 10%;
  right: 0%;
  background: #0080FF;
  color: #ffffff;
  text-align: center;
  padding: 4px;
  font-size: 200%;
  border-radius: 10px;
  display: none;
}

#next:hover {
  cursor: pointer;
  background: navy;
}

.choices {
  display: none;
}

#questions {
  font-size: 150%;
  height: 70px;
}

.pickone {
  display: inline;
  position: relative;
}

#results {
  display: none;
  border: 1px solid black;
  text-align: center;
  height: 400px;
  width: 450px;
}



#score {
  font-size: 150%;
  height: 70px;
}
<button id="start">Start </button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div id="quiz">
  <div id="questions"></div>
  <div id="answers" class="choices">
    <input type="radio" name="answers" class="choices" value=0>
    <p id="zero" class="pickone"></p>
    <br/>
    <input type="radio" name="answers" class="choices" value=1>
    <p id="one" class="pickone"></p>
    <br/>
    <input type="radio" name="answers" class="choices" value=2>
    <p id="two" class="pickone"></p>
    <br/>
    <input type="radio" name="answers" class="choices" value=3>
    <p id="three" class="pickone"></p>
  </div>
  <div id="next">next</div>
</div>
<div id="result">

</div>


你好,你能否提出一些你目前已经完成的代码相关问题吗?我会尽力在那里帮助你。 - Swati
@Swathi - 好的,我会问其他问题。谢谢您的回复。 - User35
@Swathi- 创建了一个新的问题。请看一看 https://stackoverflow.com/questions/63902525/correct-jquery-test-depend-options - User35
@Swathi - 社区已关闭了新提问。您能否请看一下 https://jsfiddle.net/vxb5mep3/1/? - User35
加入这个聊天室。 - Swati
显示剩余3条评论

0

正确的语法是 style="color: green;"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接