2014-01-28 112 views
0

我有一個數學遊戲,部分工作。我需要發生的是取div的值(一個是x,另一個是y),輸入這兩個乘數的答案,能夠提交併刷新以解決另一個問題。
任何幫助將不勝感激!如何提交輸入字段的值?

http://jsfiddle.net/justinw001/Mttw6/11/

<script type="text/javascript"> 
    function myFunction() { 
     score = 0; 
     var number = document.getElementById('inputElement').value; 
     questionAmount = number; 

     for(i = 0; i < questionAmount; i++) { 
      var x = Math.floor(Math.random() * 13); 
      var y = Math.floor(Math.random() * 13); 

      $('#input1').text(x); 
      $('#input2').text(y); 

      <!-- question = prompt('What is ' + x + ' * ' + y + ' = ?'); --> 
      question = document.getElementById('answer').value; 

      if(question == null || isNaN(question)) { 
       break; 
      } 
      if(question == x * y) { 
       score++; 
      } 
     } 

     alert('You got ' + score + ' out of ' + questionAmount + ' correct.'); 
    } 
</script> 
+1

好了,你沒有確切說明是什麼問題,但是從我所看到的,在'' HTML註釋標記不屬於那裏。您可能是指JavaScript註釋語法'/ /'。 –

+0

我應該指定多一點,我不希望頁面刷新我想要使用提交按鈕更改問題。 – user2918151

回答

0

嘗試的過程中與您的按鈕進行綁定。點擊左邊的按鈕,產生問題。 並點擊正確的,驗證答案。

演示:Fiddle

var score = 0; 
    var questions = []; 
    // Generate questions 
    $('#gen').click(function() { 
     score = 0; 
     questions = []; 
     var questionAmount = parseInt($('#inputElement').val(), 10); 
     for (var i = 0; i < questionAmount; i++) { 
      var q = { 
       x: Math.floor(Math.random() * 13), 
       y: Math.floor(Math.random() * 13) 
      }; 
      questions.push(q); 
     } 
     nextQuest(questions.pop()); 
    }); 
    // Verify the answer 
    $('#sub').click(function() { 
     var ans, x, y; 
     if (questions.length >= 0) { 
      ans = parseInt($('#answer').val(), 10); 
      x = parseInt($('#input1').text(), 10); 
      y = parseInt($('#input2').text(), 10); 
      if (ans === x * y) { 
       score++; 
       nextQuest(questions.pop()); 
      } else { 
       alert('err'); 
      } 
     } 
    }); 
    var nextQuest = function (q) { 
     if (q) { 
      $('#input1').text(q.x); 
      $('#input2').text(q.y); 
      $('#answer').val(''); 
      $('#inputElement').val(questions.length); 
     } else { 
      $('#input1, #input2').text(''); 
      $('#answer, #inputElement').val(''); 
      alert(score); 
     } 
    }; 
+0

哇!謝謝您的幫助。我不得不編輯一些東西,但是你確實讓我更容易! – user2918151