无法在Javascript测验中显示最终分数

无法在Javascript测验中显示最终分数,javascript,jquery,html,Javascript,Jquery,Html,我是Javascript的初学者 我想创建一个带有单选按钮的基本javascript测验应用程序。 在测验结束时,我希望有一条关于最终分数的提示信息。 我无法显示最终分数。 这就是我所写的。。 HTML: var-allQuestions=[{ 问题:“谁是联合王国首相?”, 选择:[“大卫·卡梅伦”、“戈登·布朗”、“温斯顿·丘吉尔”、“托尼·布莱尔”], 正确答案:0 }, { 问题:“谁是印度总理?”, 选择:[“拉胡尔·甘地”、“阿文德·凯伊里瓦尔”、“纳伦德拉·达莫达达斯·莫迪”],

我是Javascript的初学者 我想创建一个带有单选按钮的基本javascript测验应用程序。 在测验结束时,我希望有一条关于最终分数的提示信息。 我无法显示最终分数。 这就是我所写的。。 HTML:

var-allQuestions=[{
问题:“谁是联合王国首相?”,
选择:[“大卫·卡梅伦”、“戈登·布朗”、“温斯顿·丘吉尔”、“托尼·布莱尔”],
正确答案:0
},
{
问题:“谁是印度总理?”,
选择:[“拉胡尔·甘地”、“阿文德·凯伊里瓦尔”、“纳伦德拉·达莫达达斯·莫迪”],
正确答案:2
},
{
问题:“谁是美国首相?”,
选择:[“唐纳德·特朗普”、“巴拉克·奥巴马”、“希拉里·克林顿”],
正确答案:2
}
];
var=0;
$(文档).ready(函数(){
var-currentquestion=0;
$('#question').html(所有问题[currentquestion].question);
$('label[for=option0]').html(所有问题[currentquestion].choices[0]+“
”; $('label[for=option1]').html(所有问题[currentquestion].choices[1]+“
”; $('label[for=option2]').html(所有问题[currentquestion].choices[2]+“
”; $(“#下一步”)。单击(函数(){ if($((“输入[name=option]:选中”).val()==allQuestions[currentquestion].correctAnswer){ 正确答案++; }; 当前问题++; if(当前问题”; $('label[for=option1]').html(所有问题[currentquestion].choices[1]+“
”; $('label[for=option2]').html(所有问题[currentquestion].choices[2]+“
”; if(currentquestion==allQuestions.length-1){ $('#next').html(“提交”); $(“#下一步”)。单击(函数(){ /*如果我注释掉下面的If语句,但保留correctAnswers++,我确实会收到警告消息,但结果显然是错误的*/ if($((“输入[name=option]:选中”).val()==allQuestions[currentquestion].correctAnswer){ 正确答案++; } 警觉(正确答案); //警惕(“你的分数是“+currentquestion+”中的“+correctAnswers+”,你的百分比是“+(correctAnswers*100/currentquestion)+”%”; }); } }; }); });





我修复了你的代码

问题就在这里

if($("input[name=option]:checked").val()==allQuestions[currentquestion].correctAnswer){
    correctAnswers++;
}
我修复你的代码

问题就在这里

if($("input[name=option]:checked").val()==allQuestions[currentquestion].correctAnswer){
    correctAnswers++;
}

这里有一个工作演示

var-allQuestions=[{
问题:“谁是联合王国首相?”,
选择:[“大卫·卡梅伦”、“戈登·布朗”、“温斯顿·丘吉尔”、“托尼·布莱尔”],
正确答案:0
},
{
问题:“谁是印度总理?”,
选择:[“拉胡尔·甘地”、“阿文德·凯伊里瓦尔”、“纳伦德拉·达莫达达斯·莫迪”],
正确答案:2
},
{
问题:“谁是美国首相?”,
选择:[“唐纳德·特朗普”、“巴拉克·奥巴马”、“希拉里·克林顿”],
正确答案:2
}
];
$(文档).ready(函数(){
var-currentquestion=0;
var=0;
var done=false;
$('#question').html(所有问题[currentquestion].question);
$('label[for=option0]').html(所有问题[currentquestion].choices[0]+“
”; $('label[for=option1]').html(所有问题[currentquestion].choices[1]+“
”; $('label[for=option2]').html(所有问题[currentquestion].choices[2]+“
”; $(“#下一步”)。单击(函数(){ if($((“输入[name=option]:选中”).val()==allQuestions[currentquestion].correctAnswer){ 正确答案++; }; 如果(完成){ 警觉(正确答案); }否则{ 当前问题++; if(当前问题”; $('label[for=option1]').html(所有问题[currentquestion].choices[1]+“
”; $('label[for=option2]').html(所有问题[currentquestion].choices[2]+“
”; if(currentquestion==allQuestions.length-1){ $('#next').html(“提交”); 完成=正确; } } } }); });






这里有一个工作演示

var-allQuestions=[{
问题:“谁是联合王国首相?”,
选择:[“大卫·卡梅伦”、“戈登·布朗”、“温斯顿·丘吉尔”、“托尼·布莱尔”],
正确答案:0
},
{
问题:“谁是印度总理?”,
选择:[“拉胡尔·甘地”、“阿文德·凯伊里瓦尔”、“纳伦德拉·达莫达达斯·莫迪”],
正确答案:2
},
{
问题:“谁是美国首相?”,
选择:[“唐纳德·特朗普”、“巴拉克·奥巴马”、“希拉里·克林顿”],
正确答案:2
}
];
$(文档).ready(函数(){
var-currentquestion=0;
var=0;
var done=false;
$('#question').html(所有问题[currentquestion].question);
$('label[for=option0]').html(所有问题[currentquestion].choices[0]+“
”; $('label[for=option1]').html(所有问题[currentquestion].choices[1]+“
”; $('label[for=option2]').html(所有问题[currentquestion].choices[2]+“
”; $(“#下一步”)。单击(函数(){ if($((“输入[name=option]:选中”).val()==allQuestions[currentquestion].correctAnswer){ 正确答案++; }; 如果(完成){ 警觉(正确答案); }否则{ 当前问题++; if(当前问题$('#next').html("Submit"); $('#next').unbind("click"); $('#next').click(...
         /*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) + "%");