jQuery测验&横向答案

时间:2012-12-14 21:36:33

标签: jquery

我使用jQuery创建一个测验脚本,其中包含一些基于true / false的问题。每个问题都有2个选择。如果你为问题1选择了选择1的真实答案,它会自动选择选择2的假答案(横向,这是合乎逻辑的 - 你不能回答2次单个问题)。

使用我的脚本,我有一个问题,很容易说,但编码更难:如果我点击问题1的真实答案两次,我的分数增加了2倍。如果我为同一个问题选择另一个答案(在这种情况下选择1为假),则会出现同样的问题。我可以选择所有答案,但这不合逻辑......

我搜索“阻止”每个问题的“点击”选项,但我不知道如何。我和vars一起玩,但这并不令人信服。

quizz = function(){
  vars = {
     score  : 0,
     scroll : 1
  };

  // Scroll to the next question
  q = {
     next: function(which){
        if (vars.scroll === 1){
           $d.scrollTo(which, 500);
        }
     },
     // If good answer is clicked, increase the score
     score: function(which){
        if (which.hasClass("t")){
           vars.score++;
        }
     }
  };
};

// Init the quizz
quizz();

// On questions click
$(".quizz .r").on("click", function(e){
  var $t = $(this),
      $q = $t.closest(".q"),
      q_ans = $t.attr("class").split("r ")[1],
      q_id = $q.attr("id"),
      ac = "active";

  $t.addClass(ac);
  $q.find("."+ q_ans).addClass(ac);
  q.score($t);

  if (q_id == "q7"){
     e.preventDefault();
  } else {
     q.next($q.next());
  }

  $(".result p").html("Score : "+ vars.score);

   e.preventDefault();
});

以下是我的尝试:http://jsfiddle.net/uZQbS/

如果你有想法...... 提前谢谢!

2 个答案:

答案 0 :(得分:2)

这会阻止正确答案多次提高分数:

 // If good answer is clicked, increase the score
     score: function(which){
        if (which.hasClass("t")){
           vars.score++;
           which.removeClass("t"); //added
        }
     }

答案 1 :(得分:2)

刚刚找到一个使用禁用类的解决方案。

// On questions click
$(".quizz .r").on("click", function(e){
    var $t = $(this),
        $q = $t.closest(".q"),
        q_ans = $t.attr("class").split("r ")[1],
        q_id = $q.attr("id"),
        ac = "active";

    if ($q.is(":not(.disable)")){
       $t.addClass(ac);
       $q.find("."+ q_ans).addClass(ac);
       $q.addClass("disable");
       q.score($t);

       if (q_id == "q7"){
          e.preventDefault();
       } else {
          q.next($q.next());
       }

      $(".result p").html("Score : "+ vars.score);
   }

   e.preventDefault();
});
相关问题