2014-04-16 82 views
0

如何獲得currentQuestion在createQuestion() 每次點擊按鈕時,currentQuestion將更新* 1次的更新,但在createQuestion()函數,currentQuestion的價值仍然是0。覆蓋全局變量和另一個函數返回的JavaScript

var currenQuestion = 0; 

function nxtQuestion() { 
    var button = document.getElementById('nextQuestion'); 
    document.getElementById('genralCulture').addEventListener('click', function (event) { 
     if (event.target == button) { 
      currenQuestion += 1; 
      //createQuestion(); 
      event.preventDefault(); 
     } 
    }, false); 
} 

function createQuestion() { 
    var question = document.getElementById('theQuestion'); 
    if (currenQuestion < allQuestions.length) { 
     nxtQuestion(); 
     question.innerHTML = allQuestions[currenQuestion].question; 
    } else { 
     question.innerHTML = "Your score is " 
    } 
} 

在別人的話,當我點擊該按鈕,問題是沒有更新,但事件處理程序工作正常

+0

你在哪裏調用'createQuestion()'? –

+0

在html文件中。但早期,我已經調用了js文件底部的函數 –

回答

0

試試這個

var currenQuestion = 0; 

function nxtQuestion() { 
var button = document.getElementById('nextQuestion'); 
document.getElementById('genralCulture').addEventListener('click', function (event) { 
    if (event.target == button) { 
     currenQuestion += 1; 
     //createQuestion(); 
     event.preventDefault(); 
    } 
}, false); 
} 

function createQuestion() { 
currenQuestion += 1; 
var question = document.getElementById('theQuestion'); 
if (currenQuestion < allQuestions.length) { 
    nxtQuestion(); 
    question.innerHTML = allQuestions[currenQuestion].question; 
} else { 
    question.innerHTML = "Your score is " 
} 
} 
+0

不工作,問題沒有更新。 –

+0

請問您可以創建JSFiddle嗎? –

+0

http://jsfiddle.net/e7gyC/ –