Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.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中的按钮不起作用_Javascript_Jquery_Button - Fatal编程技术网

测验应用程序:javascript中的按钮不起作用

测验应用程序:javascript中的按钮不起作用,javascript,jquery,button,Javascript,Jquery,Button,目前我正在做一个选择题形式的测验。然而,当我点击一个按钮,它根本不工作 这是我的小测验的全部代码 但是我确信问题在第89-103行之间(我已经在下面粘贴了这段代码的一部分) 谁能说出问题出在哪里?有什么办法可以解决这个问题吗?更改 getTotalCorrect=$('.questionCount').text(getTotalCorrect+1) 到 getTotalCorrect=$('.questionCount').text(quick.getTotalCorrect+1)您从未设置问

目前我正在做一个选择题形式的测验。然而,当我点击一个按钮,它根本不工作

这是我的小测验的全部代码

但是我确信问题在第89-103行之间(我已经在下面粘贴了这段代码的一部分)

谁能说出问题出在哪里?有什么办法可以解决这个问题吗?

更改

getTotalCorrect=$('.questionCount').text(getTotalCorrect+1)


getTotalCorrect=$('.questionCount').text(quick.getTotalCorrect+1)

您从未设置
问题。请回答
值。该值有时是函数,有时是对象。您可能应该重命名
Question.answer
(作为
Question.setAnswer
的函数)

也就是说,
Question.answer
/
Question.setAnswer
从未被调用,而是您希望直接查看
Question.correctChoice

   if (Question.correctChoice == choice) {
        getTotalCorrect = $('.questionCount').text(getTotalCorrect+1); 
        getScore = $('.scorePercentage').text(getScore);
    }
    else {
        getTotalCorrect = $('.questionCount').text(getTotalCorrect+0); 
        getScore = $('.scorePercentage').text(getScore);
    }

如果您查看控制台,您将看到
getTotalCorrect
undefined
。这是因为
getTotalCorrect
是附加到
quick
对象范围的方法,但您正在尝试全局访问它

您可以更改此设置:

if (Question.answer == choice) {
    getTotalCorrect = $('.questionCount').text(getTotalCorrect+1); 
    getScore = $('.scorePercentage').text(getScore);
}
else {
    getTotalCorrect = $('.questionCount').text(getTotalCorrect+0); 
    getScore = $('.scorePercentage').text(getScore);
}
getNextQuestion();
致:

看起来
renderText
会无限期地调用自己:

var renderText = function(question) {
    $('.text').css('font-family', question.correctChoice);
    renderText(quiz.getCurrentQuestion());   // infinite recursion call
};
相反,在页面加载时调用renderText,如下所示:

var renderText = function(question) {
    $('.text').css('font-family', question.correctChoice);
};

renderText(quiz.getCurrentQuestion());

控制台是否显示任何错误?javascript中的按钮无法工作的一个原因可能是语义错误。控制台日志中是否有错误?我更新了代码,但在回答第一个问题后,下一个问题没有出现。我尝试查看控制台,但没有看到任何错误。非常感谢安!!!我对我的代码做了这个更正,现在我看到了分数,但它没有进入下一个问题,所以我必须解决它。希望我能代表你,但我是个新手!不客气,塞尔瓦,祝你好运-这非常接近!!看起来你必须修复
renderText
中的无限递归,然后在需要时调用它测验已初始化,并选择了一个答案。我用一些详细信息更新了我的答案。您还可以将所有显示功能分组到一个新的
测验中。原型
方法称为
displayCurrentQuestion
此外,您还可以接受堆栈Oveflow上的答案,如下所示:
question.answer = choice;    // pass the user's choice to the question object
$('.questionCount').text(quiz.getTotalCorrect());
$('.scorePercentage').text(quiz.getScore());
quiz.getNextQuestion();    // load the next question
renderText(quiz.getCurrentQuestion());    // render the new font
var renderText = function(question) {
    $('.text').css('font-family', question.correctChoice);
    renderText(quiz.getCurrentQuestion());   // infinite recursion call
};
var renderText = function(question) {
    $('.text').css('font-family', question.correctChoice);
};

renderText(quiz.getCurrentQuestion());