gpt4 book ai didi

javascript - 如何正确学习 JavaScript 测验问题

转载 作者:行者123 更新时间:2023-12-03 12:30:37 24 4
gpt4 key购买 nike

我正在处理How to Learn JavaScript Correctly 。大约进行到一半时,您将创建一个测验应用程序。我能够让它显示第一个问题,然后单击下一步按钮更改为第二个问题。但问题是,当我第三次单击该按钮时,屏幕会清除,并且第三个问题永远不会出现。

我确信我错过了一些简单的事情。知道我哪里出错了吗?

app.js:

var allQuestions = [
{question: "Who is Prime Minister of the United Kingdom?",
choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
correctAnswer:0},
{question: "Who is President of the United States?",
choices: ["George Bush", "Barack Obama", "Hilary Clinton"],
correctAnswer:1},
{question: "What is the best state?",
choices: ["Iowa", "Wisconsin", "Colorado", "North Carolina"],
correctAnswer:1}
];

var score = 0;
var i = 0;


$(document).ready(function() {
$('#next').addClass('hidden');

nextQuestion();

});

function nextQuestion() {
var container = $('#container');

var questionName = allQuestions[i].question
var answer = allQuestions[i].correctAnswer;
var choice = 0;

var question = "<div>" + questionName + "</div>";
container.append(question + "<br>");
var choices = allQuestions[i].choices;
for (var j=0;j<choices.length;j++) {
var choice = choices[j];
var radio = "<input type='radio' data-choice='" + j + "' value='" + choice + "' name='" + allQuestions[i].question + "'>" + choices[j];
container.append(radio + "<br>");
}

$('input:radio').on('click',function() {
choice = $(this).data("choice");
$('#next').removeClass('hidden');

});

$('#next').on('click',function() {
$('#next').addClass('hidden');
if (choice === answer) {
alert("Winner!");
}
if (i < allQuestions.length) {
i += 1;
}
container.empty();
nextQuestion();

});
}

索引.html:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Survey</title>
<link rel="stylesheet" href="assets/bootstrap.min.css">
<link rel="stylesheet" href="assets/bootstrap.responsive.min.css">
<link rel="stylesheet" href="assets/base.css">

</head>
<body>
<!-- index.html -->
<div id="container">



<script src="js/lib/jquery.min.js"></script>
<script src="js/lib/underscore-min.js"></script>
<script src="js/lib/bootstrap.min.js"></script>
<script src="js/app.js"></script>
</div>
<input type='submit' value='Next' id='next'>
</body>
</html>

最佳答案

从函数中删除以下代码;您多次绑定(bind)事件。有时,当您单击#next一次时,它可能会前进多次,因为它响应多个绑定(bind)。

$('input:radio').on('click',function() {
choice = $(this).data("choice");
$('#next').removeClass('hidden');

});

$('#next').on('click',function() {
$('#next').addClass('hidden');
if (choice === answer) {
alert("Winner!");
}
if (i < allQuestions.length) {
i += 1;
}
container.empty();
nextQuestion();

});

将代码放入 DOM 就绪事件中,并进行如下所示的修改:

$(function() {
$(document).on('change', 'input:radio', function() {
choice = $(this).data("choice");
$('#next').removeClass('hidden');

});

$('#next').on('click',function() {
$('#next').addClass('hidden');
if (choice === answer) {
alert("Winner!");
}
if (i < allQuestions.length) {
i += 1;
}
container.empty();
nextQuestion();

});
});

关于javascript - 如何正确学习 JavaScript 测验问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23946769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com