测验应用程序:javascript中的按钮无效

时间:2015-07-07 22:31:32

标签: javascript jquery button

目前我正在进行多种选择格式的测验。但是,当我点击一个按钮时,它根本不起作用!

这是我的迷你测验的完整代码 https://github.com/selvabalasingam/TypeQuiz/blob/master/js/app.js

但我确定问题出在第89-103行之间(我在下面粘贴了部分代码)

$('button.choice').click(function(e) {
    var choice = $(e.target).text();
    var question = quiz.getCurrentQuestion(); 
    // check if the answer is right and update question number and score
    if (Question.answer == choice) {
        getTotalCorrect = $('.questionCount').text(getTotalCorrect+1); 
        getScore = $('.scorePercentage').text(getScore);
    }
    else {
        getTotalCorrect = $('.questionCount').text(getTotalCorrect+0); 
        getScore = $('.scorePercentage').text(getScore);
    }
    // then go to the next question
    getNextQuestion();
});

谁能说出问题是什么?有没有办法解决这个问题?

3 个答案:

答案 0 :(得分:1)

更改

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

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

答案 1 :(得分:0)

您永远不会设置Question.answer值。该值有时是一个函数,有时也是一个对象。您应该重命名Question.answer(作为Question.setAnswer的函数。

from this gist

Question.prototype = {
    setAnswer: function(choice) {
        this.answer = choice;
    },
}

话虽如此,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);
    }

答案 2 :(得分:0)

如果你看一下控制台,你会发现getTotalCorrectundefined。这是因为getTotalCorrect是附加到quiz对象范围的方法,但您尝试全局访问它。

你可以改变这个:

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

为:

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

看起来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());