2013-03-31 21 views
1

你好我即时尝试做一个动态测验,以学习JavaScript/jQuery的/ HTML和CSS ...我卡住,因为我只是添加一个后退按钮,这回按钮,用户可以回去查看他的最后一个答案,并且也改变了,如果最后的答案被正确地回答了,那么它的确得分 - ;但如果没有正确回答,就不会有任何勇气,以避免人们获得无限的分数。我真正的问题是,即时通讯使用lastValue变量来存储拉斯单选按钮,所以当你回去它可以比较,但它只是工作正常,我你回去一次,然后lastValues不更新,所以行为是奇怪的,这是即时通讯的代码使用比较值的和平:获得与jquery测验中使用的最后一个值

function backQuestion(){ 
    if (number == 9){ 
     $('#myForm').css('display', 'inline-block'); 
     $('#question').css('display', 'block'); 
     $('#next').css('display', 'inline-block'); 
     $('#score').css('display', 'none'); 
    } 

    if(number > 0){ 
     number --; 
     addQuestionAndAnswers(); 
     if (lastValue == allQuestions[number].answer){ 
      score --; 
      console.log('your score after going back is: ' + score); 
      console.log('the last value is: ' + lastValue); 
      console.log('this is the value of number: ' + number); 
     }else{ 
      //lastValue = 
      console.log('your score after going back is: ' + score); 
      console.log('the last value is: ' + lastValue); 
      console.log('this is the value of number: ' + number); 
     } 
    } 


} 

我也离开了JS提琴演示,所以你可以检查代码和变量的其余部分。在我写这篇文章时,我只是想将响应的值存储在数组中,然后只是每个响应都将这个值存储在数组中,然后当你按下按钮时可以获得该值以进行比较,那么你可以只是删除该勇气,如果答案再次回答....即时通讯不知道这是一种复杂的方式...不理解,如果有人可以告诉我或建议我一个简单的方法ty :)

js小提琴:http://jsfiddle.net/xtatanx/Wn8Qg/2/

爵士小提琴结果全屏:http://jsfiddle.net/xtatanx/Wn8Qg/2/embedded/result/

+0

该死..不得不回答10个问题去'back'按钮。 – SachinGutte

+0

后退按钮始终可用! –

回答

1

我会推荐存储所有的答案,就像这样:

http://jsfiddle.net/Wn8Qg/4/

$(ready); 

function ready(){ 
    var allQuestions = 
    [ 
     { 
      question: "Whats my real name?", 
      choices: ["Jhonnatan", "Alberto", "Tatan","Jaime"], 
      answer: 0 
     }, 

     { 
      question: "Who is Colombia's president?", 
      choices: ["Alvaro Uribe", "Andres Pastrana", "Juan Manuel Santos","Tatan"], 
      answer: 2 
     }, 

     { 
      question: "My favorite super heroe?", 
      choices: ["Batman", "Flash", "Tatan","Javascript"], 
      answer: 3 
     }, 

     { 
      question: "Wich sports do i practice?", 
      choices: ["Climbing", "Swimming", "Programming","Running"], 
      answer: 0 
     }, 

     { 
      question: "Whats my dad's name?", 
      choices: ["Alberto", "Jorge", "Javier","Jose"], 
      answer: 1 
     }, 

     { 
      question: "Whats my favorite color?", 
      choices: ["Red", "Purple", "Blue","All"], 
      answer: 2 
     }, 

     { 
      question: "My favorite alcoholic drink", 
      choices: ["Vodka", "Aguardiente", "Rum","Tekila"], 
      answer: 3 
     }, 

     { 
      question: "Whats my favorite kind of music?", 
      choices: ["Hardcore", "Reggaeton", "Salsa","Programming"], 
      answer: 0 
     }, 

     { 
      question: "How many qestions has this quiz??", 
      choices: ["20", "8", "10","12"], 
      answer: 2 
     }, 

     { 
      question: "My favorite programming lenguage?", 
      choices: ["Ruby", "Arduino", "Python","Javascript"], 
      answer: 3 
     } 
    ]; 

    var score = 0; 
    var number = 0; 
    var question = $('#question'); 
    var choice1 = $('#answer0'); 
    var choice2 = $('#answer1'); 
    var choice3 = $('#answer2'); 
    var choice4 = $('#answer3'); 
    var next = $('#next'); 
    var back = $('#back'); 
    var currentQuestion = $('#current-question'); 

    var answers = new Array(allQuestions.length); 

    next.click(on_click_next); 
    back.click(on_click_back); 

    populate(); 

    function populate() { 
     currentQuestion.text(number + 1); 

     question.text(allQuestions[number].question); 

     choice1.text(allQuestions[number].choices[0]); 
     choice2.text(allQuestions[number].choices[1]); 
     choice3.text(allQuestions[number].choices[2]); 
     choice4.text(allQuestions[number].choices[3]); 

     $(":radio").prop('checked', false); 

     if (answers[number] !== null) 
      $(":radio").eq(answers[number]).prop('checked', true); 
    } 

    function on_click_next(){ 
     if($(":radio:checked").length == 1) 
     { 
      answers[number] = $(":radio:checked").val(); 

      number++; 

      if (number < allQuestions.length) { 
       populate(); 
      } else { 
       displayResult(); 
      } 
     }else{ 
      alert('please select an answer before proceed'); 
     } 

     function displayResult(){ 
      var score = get_score(); 
      currentQuestion.text(allQuestions.length); 

      $('#question, #alternatives').hide(); 
      $('#next').hide(); 
      $('#score').show(); 
      $('#score').text('Your score is: ' + score + 'pts'); 
     } 
    } 

    function get_score() 
    { 
     var result = 0; 
     for(var i = 0; i < answers.length; i++) 
      if (allQuestions[i].answer == answers[i]) 
       result++; 

     return result; 
    } 

    function on_click_back() 
    { 
     if (number == allQuestions.length) 
     { 
      $('#question, #alternatives').show(); 
      $('#next').show(); 
      $('#score').hide(); 

      number--; 

      populate(); 
     } 
     else 
     { 
      if(number > 0) 
      { 
       number--; 
       populate(); 
      } 
     } 
    } 
} 
+0

感谢您花时间阅读代码,并做出了一个......我真的很感激你的代码比我的代码更健壮,正如我所说我正在学习javaScript,总有一天我会想要像你的代码。现在我要研究它,以便在实际代码中使用它,然后我将试着做更强大的所有代码!....我已经在存储中考虑了数组中的答案,但方法我在想,似乎更痛苦:p –

相关问题