我正在构建测验应用程序,但无法验证用户答案是否正确。测验包含10个问题,并使用单选按钮从用户那里获得答案。对于某些问题,没有一个答案注册为正确,而对于其他一些问题,所有答案都注册为正确。
每个问题都有自己的'questionClass',如下所示:
function questionClass (q, a1, a2, a3, a4, n, c) {
this.question = q;
this.answer1 = a1;
this.answer2 = a2;
this.answer3 = a3;
this.answer4 = a4;
this.number = n;
this.correct = c;
每个问题类都使用以下公式创建:
var questionOne = new questionClass("quotation to be analyzed", 'answer1','answer2','answer3','answer4', number, 'correct');
questions.push(questionOne);
验证用户是否得到正确答案的功能如下:
this.checkAnswer=checkAnswer;
function checkAnswer() {
if ($('input:radio[name=" ' + this.number + ' "]:checked').val() == this.correct) {
score++;
}
else if ($('input:radio[name=" ' + this.number + ' "]:checked').val() == this.correct) {
score++;
}
else if ($('input[id=" ' + this.answer3 + ' "]:checked') && (this.answer3 == this.correct)) {
score++;
}
else if ($('input[id=" ' + this.answer4 + ' "]:checked') && (this.answer4 == this.correct)) {
score++;
}
else {
console.log("you got the wrong answer");
}
}
在问题之间移动的功能如下:
$("#continue").click(function(){
counter++;
if (counter == 1) {
questions[x].display();
}
else if ((counter >= 1) && (counter <= 10)) {
if (questions[x].isSelected()) {
$('.warning').html("");
questions[x].checkAnswer();
$('.score').html("<p>Your score is " + score + " out of 10</p>");
x++;
if (x < 10){
questions[x].display();
}
}
}
else if ((counter >= 11) && (!questions[x].isSelected())) {
return;
}
else {
$('.warning').html("");
questions[x].checkAnswer();
$('.score').empty();
$('#container').html('<h1>You scored ' + score + ' out of 10.</h1></br>');
$('#container').append('<input type="button" id="tryAgain" value="Try Again!">');
}
});
可以在以下链接查看该应用程序:
http://dl.dropboxusercontent.com/u/91499081/QuizApp/quizApp.html