我有一个对象数组,我想获取其中一个对象并从对象内容创建一个单选按钮列表。到目前为止,这是我的代码。
var allQuestions = [{question: "This is question number one", choices: ["one", "two", "three", "four"], correctAnswer:"two"},{question: "This is question number two", choices: ["dog", "cat", "bear", "lion"], correctAnswer:"bear"}];
var currentQuestion = allQuestions[1].question;
document.getElementById('question').innerHTML = currentQuestion;
function choiceList() {
for (choices in allQuestions[0]) {
var choiceSelection = document.createElement('input');
choiceSelection.setAttribute('type', 'radio');
choiceSelection.setAttribute('name', 'choice');
document.getElementById('answersBox').innerHTML = choiceSelection;
}
}
这是我的 HTML:
<body>
<form>
<label id="question">Question:</label><br />
<div id="answersBox">
</div>
<input type="button" value="save" />
</form>
<script src="scripts.js"></script>
</body>
问题是,单选按钮没有显示在 answersBox div 中。