0
我正在嘗試使用單選按鈕選項進行簡單測驗。測驗有一個按鈕,應該動態刪除並添加下一個問題,並設置單選按鈕。問題和選項存儲在單獨的數組中。我被困在試圖用單選按鈕連接每個選項。任何想法如何去解決這個問題?如何使用單選按鈕連接數組元素
P.S.剛開始學習JS,你可能會從我的代碼中知道
<script type="text/javascript">
var questionsArray = new Array();
questionsArray[0] = "1. Who's the president of the US?";
questionsArray[1] = "2. What is the capital city of France?";
questionsArray[2] = "3. What is your favorite food?";
var choicesArray = new Array();
choicesArray[0] = ["Lebron James", "Barack Obama", "George Bush","George Clooney"];
choicesArray[1] = ["Nairobi","London","Paris","Sydney"];
choicesArray[2] = ["Pizza","Sushi","Pasta","Lobster"];
function createRadioElement(name){
var radioHTML = '<input type="radio" name="'+name+'"'+'/>';
var radioFragment = document.createElement("div");
radioFragment.innerHTML = radioHTML;
return radioFragment.firstChild;
}
var index = -1;
function questionSwapOnclick(){
while(index < questionsArray.length){
index++;
document.getElementById("question").innerHTML = questionsArray[index];
if(document.getElementById("question").innerHTML == "undefined"){
document.getElementById("question").innerHTML = "Finished the quiz!"
document.getElementById("choices").innerHTML = ""
}
var splitChoices = choicesArray[index].join("<br />");
document.getElementById("choices").innerHTML = splitChoices;
return true;
}
}
</script>
寫一個數組做'questionsArray = 「1誰是美國總統?」, 「2什麼是法國的首都?」;」 ......「]' – maioman