我正在尝试创建一个测验程序。单击按钮,每个问题对象都会加载到页面。我有一个代码,但它无法正常工作。不确定什么是错的。任何人都可以帮忙。我想用按钮点击控制循环,这样我得到第一个问题,然后点击和下一个问题,依此类推。
var quiz = document.quiz;
var nextButton = document.getElementById('nextButton');
var questions =
[
{
question: "What is the color of skye?",
choices : ["Blue", "White", "Yellow", "Green"],
answer : 0
},
{
question: "What is the color of skye?",
choices : ["Blue", "White", "Yellow", "Green"],
answer : 0
}
];
var counter = 0;
nextButton.onclick = function () {
while ( counter < questions.length ) {
var question = questions[counter].question;
var choices = questions[counter].choices;
var answer = questions[counter].answer;
answer = choices[answer];
print( question, choices, answer );
counter++;
}
};
function print ( question , choices , answer ) {
var html = "";
html += "</h3>" + question + "</h3>";
for ( var option in choices ) {
html += "<input type='radio' name='option'>" + choices[option] + "</input>";
html += "<br>";
}
quiz.innerHTMl = html;
}
<!DOCTYPE html>
<html>
<head>
<title> Quiz </title>
</head>
<body>
<form action="" name="quiz"></form>
<form action="" name="controls">
<input type="button" value="Next" id="nextButton">
</form>
<script src="quiz.js"></script>
</body>
</html>
答案 0 :(得分:2)
以下是您的代码https://jsfiddle.net/r1kcL1pc/
的示例static final String ONE = "one";
static final String TWO = "two";
此行必须更改
quiz.innerHTMl = html;
只有一封信:)
然后,我删除了while cicle,因此您可以在每次单击按钮时更改问题。