我一直試圖弄清楚這一點,而我完全難住。如何使用onclick進行循環提前?
我正在編寫一個程序,該程序應該顯示一系列基本的多項選擇題。你看到一個問題,你點擊其中一個答案,然後轉到下一個問題。
問題是,我無法弄清楚如何顯示一個問題,然後在用戶單擊其中一個按鈕時顯示下一個問題。當我點擊一個按鈕時沒有任何反應。出了什麼問題?
// progress meter
var progress = new Array();
for (var i = 0; i < questions.length; i++) progress.push("0");
var i = 0;
display(0);
// display questions
function display(i) {
var prg_string;
for (var j = 0; j < progress.length; j++) prg_string += progress[j];
document.write(
"<div id = 'background'>"
+ "<div id = 'progress'>" + progress + "</div>"
+ "<div id = 'title'>-JogNog Test v1-<br></br>" + tower + "</div>"
+ "<div id = 'question'>" + questions[i].text + "</div>"
+ "<div id = 'stats'>Level " + level + "/" + total_levels + " Question " + (i + 1) + "/" + questions.length + "</div>"
+ "</div>"
);
document.write("<button id = 'answer1' onclick = 'next(questions[i].answers[0].correct)'>" + questions[i].answers[0].text + "</button>");
if (questions[i].answers.length > 0)
document.write("<button id = 'answer2' onclick = 'next(questions[i].answers[1].correct)'>" + questions[i].answers[1].text + "</button>");
if (questions[i].answers.length > 1)
document.write("<button id = 'answer3' onclick = 'next(questions[i].answers[2].correct)'>" + questions[i].answers[2].text + "</button>");
if (questions[i].answers.length > 2)
document.write("<button id = 'answer4' onclick = 'next(questions[i].answers[3].correct)'>" + questions[i].answers[3].text + "</button>");
}
// go to next question, marking whether answer was right or wrong
function next(correct) {
if(correct) progress[i] = "T";
else progress[i] = "F";
i += 1;
display(i);
}
你可以發佈一個jsfiddle顯示該問題嗎? – jbabey 2012-08-15 12:43:43
太長;沒有閱讀... – Alnitak 2012-08-15 12:46:11
jsFiddle:http://jsfiddle.net/tTP8z/ – 2012-08-15 12:46:50