【发布时间】:2017-10-30 15:07:04
【问题描述】:
我现在正在创建一个测验应用程序。
现在我正处于为数组的每个值创建单选和标签元素的阶段,但我显然没有做正确的事情,因为当我单击按钮调用单击事件时它不起作用,没有单选按钮或标签附加到选定的 div。
非常感谢您的帮助。
var dude = [{
question: "What is dudes's favourte color?",
choices: ["blue", "yellow", "red", "green"],
answer: 0
}, {
question: "What is dudes's favourte movie?",
choices: ["Scarface", "The Terminator", "Shawshank Redemption", "The Dark Knight"],
answer: 3
}, {
question: "What was dudes's first ever job role?",
choices: ["Cleaner", "Store Assistant", "Sales", "Admin"],
answer: 1
}]
var currentQuestion = 0;
var questionNumber = 1;
dude[currentQuestion].choices.forEach(function(value) {
var radio = document.createElement("input");
var label = document.createElement("label");
radio.setAttribute("type", "radio");
radio.setAttribute("name", "answer");
radio.setAtrribute("value", value);
label.setAtrribute("for", value);
quizSectionDiv.appendChild(radio);
quizSectionDiv.appendChild(label);
})
for each 方法位于一个点击事件中,该事件适用于它所包含的其他代码行
【问题讨论】:
-
我认为这里没有足够的信息来告诉您出了什么问题。考虑制作一个 jsfiddle 来说明您的问题。
-
这里有足够的信息告诉你出了什么问题,@jimmy118。看我的回答和解释。
标签: javascript html