-5

我很确定我错过了一些简单的事情,但是根据是否选择了正确的答案,分数并没有正确计算。它是一个多项选择测验。该代码是由其他人编写的,因此请尝试单独解决此问题!有任何想法吗?


var Question = function(Text){
  this.Question = Text;
  this.Answers = [];
  this.addAnswer = function(Answer, Correct){
    if(typeof Correct == 'undefined') Correct = false;
    else Correct = Correct == true;
    this.Answers.push({"Answer":Answer, "Correct":Correct});
  };
  this.checkAnswer = function(Index){
    if(typeof this.Answers[Index] == 'undefined') return false;
    return this.Answers[Index].Correct;
  };
};


var Questions = [];
var q;
var Score = 0;
var CurrentQuestion = -1;

var nextQuestion = function()
{
  ++CurrentQuestion;

  if(typeof Questions[CurrentQuestion] == 'undefined') return false;
  var Question = Questions[CurrentQuestion];

  $('#QuestionIndex').html(Numbers[CurrentQuestion]);
  $('#QuestionsRemaining').html((Questions.length-CurrentQuestion-1));
  $('#Question').html(Question.Question);
  $('#Answers').html('');


  for(var t in Question.Answers)
  {
    i = t;
    t = Question.Answers[t];
    var Div = $('<div class="answer"></div>');
    $(Div).append('<span class="radioContainer"><input type="radio" name="Answer" id="answer_'+ i +'"></span> ');
    $(Div).append($('<label for="answer_' + i + '" />').html(t.Answer));
    $('#Answers').append(Div);
  }

  $("input[type='radio']").change(function(){
    $('.radioContainer').css('background-image', 'none');

    attr = $(this).attr('checked');
    if(typeof attr !== 'undefined' && attr !== false)
    {
   $(this).parent().css('background-image', 'url("http://dev.clickymedia.co.uk/fbart/wp-content/themes/artbook/img/tick.png")');
    }
  });

  return true;
}

//Returning false should stop the user from progressing to the next question.
var checkCurrentAnswer = function()
{
  if(typeof Questions[CurrentQuestion] == 'undefined') return false;
  var i = $('#Answers input:checked').parent().index();
  if(i < 0) return false;

  if(Questions[CurrentQuestion].checkAnswer(i) == true) Score++; 

  return true;
}

q = new Question("Which famous film star did Pop artist Andy Warhol make more portraits of than any other?");
q.addAnswer('Marilyn Monroe', true);
q.addAnswer('Sophia Loren');
q.addAnswer('Audrey Hepburn');
q.addAnswer('Doris Day');
Questions.push(q);

q = new Question("The Mona Lisa, Leonardo da Vinci's magnum opus, draws crowds into which famous European museum?");
q.addAnswer('The Mus&#233;e du Louvre, Paris', true);
q.addAnswer('The Tate Britain, London');
q.addAnswer('The National Gallery, Berlin');
q.addAnswer('The Uffizi Gallery, Florence');
Questions.push(q);

$(function(){
  nextQuestion();

  $('#nextQuestion').bind('click', function(){
    if(checkCurrentAnswer())
    {
      if(nextQuestion())
      {

      }else{ //End of examination! - Put your pens and pencils down.
        $('#QuestionBook .FinalLeft .Score').html(Score + '/' + Questions.length);

        $('#QuestionBook .FinalLeft .Message').html(Message);


        $('#QuestionBook .QuestionContainer, #QuestionBook .AnswersContainer').hide();
        $('#QuestionBook .FinalLeft, #QuestionBook .FinalRight').show();


      }
    }
  });
});

4

1 回答 1

2

您的checkCurrentAnswer函数正在计算错误的索引。具体来说,这一行:

var i = $('#Answers input:checked').parent().index();

您的 HTML 如下所示:

<div id="Answers">
  <div class="answer">
    <span class="radioContainer"><input type="radio" /></span>
    <label for="answer_0">The Musée du Louvre, Paris</label>
  </div>
  <div class="answer">
    <span class="radioContainer"><input type="radio" /></span>
    <label for="answer_1">The Tate Britain, London</label>
  </div>
  ...
</div>

因此,i始终0,因为radio输入的父项始终是 a span,它始终是 的第一个子项(索引0answer。你应该index用类来计算 div 的数量answer

要修复,它很简单,而不是使用parent(),使用closest(".answer")

var i = $('#Answers input:checked').closest(".answer").index();
于 2012-09-03T22:38:51.377 回答