我正在寫一個JavaScript程序來動態地打印多維數組的元素來創建測驗。我的結構是正確的,但我試圖打印'選擇'元素,因此它們顯示爲用於選擇每個問題的單選按鈕。使用for循環動態打印多維數組元素
的Javascript:
var dataquestions = [];
dataquestions[0] = {question: "Who is the prime Minister of the United Kingdom?", choice0: "David Cameron", choice1: "Tony Blair", choice2:"Gordon Brown", answer:"David Cameron"};
dataquestions[1] = {question:"In what year was the Declaration of Independence signed?", choice0:"1985", choice1:"1492", choice2:"1776", answer:"1776"};
dataquestions[2] = {question:"Which country is in Europe?", choice0:"Pakistan", choice1:"France", choice2:"Australia", answer:"France"};
for (var i=0; i<=2; i++)
{
document.getElementById('quizcontain').innerHTML+= '<p>'+ dataquestions[i].question+'</p><br><br>';
for (var j=0; j<=2; j++){
document.getElementById('quizcontain').innerHTML+='<input type="radio" name='+j+'>'+dataquestions[i].choice+j+'<br>';
}
}
輸出:
Who is the prime Minister of the United Kingdom?
undefined
undefined
undefined
In what year was the Declaration of Independence signed?
undefined
undefined
undefined
Which country is in Europe?
undefined
undefined
undefined
完美地工作!謝謝! –
@JeffP。歡迎您:)請忘記不要打勾這個答案:) – thefourtheye
不錯,@thefourtheye。 – zee