Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript js/jquery赋值的语法问题-对象中的测验问题_Javascript_Jquery - Fatal编程技术网

Javascript js/jquery赋值的语法问题-对象中的测验问题

Javascript js/jquery赋值的语法问题-对象中的测验问题,javascript,jquery,Javascript,Jquery,我正试图通过一项作业来加深对js的理解,但我遇到了一些问题,这些问题阻碍了我完成最终的代码。该代码执行一个简短的测验-从单选按钮输入并匹配到包含答案的对象,然后输出最终分数 代码在 我现在意识到的问题: 我的$response变量似乎不起作用 var $response = $('[name=rad]:checked').val(); 柜台正在通过问题收听点击。在最后一个问题之后,我想报告最后的分数。我无法让计数器到达问题+1的末尾,并且我无法获得听众报告的准确最终分数(var finalSc

我正试图通过一项作业来加深对js的理解,但我遇到了一些问题,这些问题阻碍了我完成最终的代码。该代码执行一个简短的测验-从单选按钮输入并匹配到包含答案的对象,然后输出最终分数

代码在

我现在意识到的问题: 我的$response变量似乎不起作用

var $response = $('[name=rad]:checked').val();
柜台正在通过问题收听点击。在最后一个问题之后,我想报告最后的分数。我无法让计数器到达问题+1的末尾,并且我无法获得听众报告的准确最终分数(var finalScore)


这些只是片段,所以请查看完整的代码。我想要一些关于如何理解我错在哪里的建议

以下是一种方法:

//Initialization
var counter=0;
var score=0;

loadQuestion();
$('#next').on('click',answer);


//functions

function answer(){
    // if the user did not answer
    if ($('input:radio:checked').length == 0){
        alert('You have to answer!');
        return;
    }
    var currentQ=questions[counter];
    // if answer is correct
    if($('[name=rad]:checked').val()==currentQ.answer){score++;}
    counter++;
    // if there are no questions left
    if(counter >= questions.length) {displayResults(); return;}
    loadQuestion();
}

function loadQuestion(){
    // clear the radio buttons
    $("input:checked").removeAttr("checked");
    var currentQ=questions[counter];
    // display the question
    $('h1').text(currentQ.question);
    $('#a1').text(currentQ.choices[0]);
    $('#a2').text(currentQ.choices[1]);
    $('#a3').text(currentQ.choices[2]);
}

function displayResults(){
    $("h1").text("You've finished with a score of " + score + "!");
    $('[name=rad], #a1, #a2, #a3, #next').remove();
}

不是解决方案,但您的所有单选按钮都具有相同的id“rad”。ID必须是唯一的!也不是解决方案,但您不需要
parseInt()
一个已经是整数的变量。哈,谢谢大家的帮助:)我不认为需要parseInt,但我从另一个学生的小提琴样本中挑选了它。我已经删除了它并更新了id。在单击按钮之前不要查找该值<代码>$response在用户更改RadioBlex时不会自动更新这很好-我只想做一次编辑。我想替换屏幕上的副本,而不是提醒最终分数。类似于:函数displayResults(){$(“h1”).text(“您的分数为“+finalScore+”!”);$(“#a1”).remove();$(“#a2”).remove();$(“#a3”).remove();$(“[name=rad]”).remove();});但这似乎不起作用……好了,达恩!请注意,您可以将这些
.remove()
合并在一起。我还更新了JSFiddle.property!非常感谢。现在我似乎更擅长阅读js而不是写作。我想熟能生巧。至少你的解决方案对我有意义。再次感谢:)看起来你的评论中有额外的括号和括号。是的,将其分解为明确命名的函数可以使其更易于阅读和调试。
//Initialization
var counter=0;
var score=0;

loadQuestion();
$('#next').on('click',answer);


//functions

function answer(){
    // if the user did not answer
    if ($('input:radio:checked').length == 0){
        alert('You have to answer!');
        return;
    }
    var currentQ=questions[counter];
    // if answer is correct
    if($('[name=rad]:checked').val()==currentQ.answer){score++;}
    counter++;
    // if there are no questions left
    if(counter >= questions.length) {displayResults(); return;}
    loadQuestion();
}

function loadQuestion(){
    // clear the radio buttons
    $("input:checked").removeAttr("checked");
    var currentQ=questions[counter];
    // display the question
    $('h1').text(currentQ.question);
    $('#a1').text(currentQ.choices[0]);
    $('#a2').text(currentQ.choices[1]);
    $('#a3').text(currentQ.choices[2]);
}

function displayResults(){
    $("h1").text("You've finished with a score of " + score + "!");
    $('[name=rad], #a1, #a2, #a3, #next').remove();
}