當用戶點擊按鈕時,它將驗證是否有任何單選按鈕被選中(checkChoices()
),如果是,它將比較選中的單選按鈕的值和correctAnswer
。函數不增加給定的變量
問題在於keepScore()
函數,因爲score
對於任何答案都爲0。
var score = 0;
var checkChoices = function(){
for (var i = 0; i < choices.length; i++) {
if (choices[i].checked) {
var value = choices[i].value;
choices[i].checked = false;
}
}
return value;
};
//the keepScore function doesn't increment
var keepScore = function(){
if(checkChoices() == allQuestions[y].correctAnswer) {
score++;
}
};
var behavior = function(){
if(y<allQuestions.length-1 && checkChoices()) {
keepScore();
y++;
}
};
nextBtn.onclick = function(){
behavior();
};
你確定你甚至調用'keepScore()'函數嗎?在你的按鈕上,你應該使用'type =「按鈕」'。 – 2014-11-03 21:42:17
您是否閱讀過該代碼?我在'behavior()'函數中調用了它。是的,我使用'type ='button'',但它不會有所作爲,因爲它不在表單中。 – GSG 2014-11-03 21:45:26
「行爲」中的「y」是什麼? – georg 2014-11-03 21:54:25