我是Java语言的初学者
我想创建一个带有单选按钮的基本javascript测验应用程序。
测验结束时,我想发出一条警告消息,告知最终成绩。
我无法显示最终分数。
这是我编码的。
HTML:
var allQuestions = [{
question: "Who is the Prime Minister of the United Kingdom?",
choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
correctAnswer: 0
},
{
question: "Who is the Prime Minister of India?",
choices: ["Rahul Gandhi", "Arvind Kejrival", "Narendra Damodardas Modi"],
correctAnswer: 2
},
{
question: "Who is the Prime Minister of America?",
choices: ["Donald Trump", "Barack Obama", "Hilary Clinton"],
correctAnswer: 2
}
];
var correctAnswers = 0;
$(document).ready(function() {
var currentquestion = 0;
$('#question').html(allQuestions[currentquestion].question);
$('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>");
$('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>");
$('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>");
$("#next").click(function() {
if ($("input[name=option]:checked").val() == allQuestions[currentquestion].correctAnswer) {
correctAnswers++;
};
currentquestion++;
if (currentquestion < allQuestions.length) {
$('#question').html(allQuestions[currentquestion].question);
$('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>");
$('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>");
$('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>");
if (currentquestion == allQuestions.length - 1) {
$('#next').html("Submit");
$('#next').click(function() {
/*If I comment out the following if statement, but retain correctAnswers++, I do get alert message,but the result is obviously wrong*/
if ($("input[name=option]:checked").val() == allQuestions[currentquestion].correctAnswer) {
correctAnswers++;
}
alert(correctAnswers);
//alert("Your Score is " + correctAnswers + " out of " + currentquestion + " and your percentage is " + (correctAnswers*100/currentquestion) + "%");
});
}
};
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<body>
<div>
<h3 id="question"></h3>
<form id="form">
<input type="radio" name="option" value="0" id="option0" checked>
<label for='option0'>
<br/>
</label>
<input type="radio" name="option" value="1" id="option1">
<label for='option1'>
<br/>
</label>
<input type="radio" name="option" value="2" id="option2">
<label for='option2'>
<br/>
</label>
</form>
</div>
<br/>
<a href="#" id="next" class="button hvr-bounce-to-right">Next</a>
</body>
请帮忙!
这是JSFiddle。
最佳答案
如果您注释if和correctAnswers ++句子,则您的代码正确运行。
/*If I comment out the following if statement, but retain correctAnswers++, I do get alert message,but the result is obviously wrong*/
/*if ($("input[name=option]:checked").val() == allQuestions[currentquestion].correctAnswer) {
correctAnswers++;
}*/
alert(correctAnswers);
alert("Your Score is " + correctAnswers + " out of " + currentquestion + " and your percentage is " + (correctAnswers*100/currentquestion) + "%");