简体   繁体   English

未捕获的TypeError:无法读取未定义的属性'NaN'

[英]Uncaught TypeError: Cannot read property 'NaN' of undefined

I am trying to set the variable qnum to add one number when the user selects the correct answer. 我正在尝试将变量qnum设置为在用户选择正确答案时添加一个数字。 As you see, I set 'qnum' as a global variable and then try to initiate it's value in my code for function displayCurrentQuestion, and in other places in my code. 如您所见,我将'qnum'设置为全局变量,然后尝试在代码中为函数displayCurrentQuestion和其他地方初始化它的值。 Everytime I do this, I keep getting the error about not being able to read the property 'NaN', which I know means not a number, but why would that come up here. 每次执行此操作时,我都会不断收到无法读取属性“ NaN”的错误,我知道这不是数字,而是为什么会出现在这里。 Esspecially when I set the global variable to '0'! 特别是当我将全局变量设置为“ 0”时!

My site is available at http://keithand.github.io/quiz-app/ . 我的网站位于http://keithand.github.io/quiz-app/

$(document).ready(function() {

//---------GLOBAL VARIABLES

var complete = false; // will become true after quiz is completed
var score = 0;
var response; // the users answer
var qnum = 0; // keeps track of what question number the user is on
var result; //the value if the user answered correctly
var answered; //stores the value of whether the user has already answered the question

var correctAnswer = 0;
var wrongAnswer = 0;

var question = [
        {
        num: 1,
        quote : "Aren't we forgetting the true meaning of Christmas: the birth of Santa.",
        ans : 'Bart Simpson',
        correct: 2
        },
        {
        num: 2,
        quote : "I know how hard it is for you to put food on your family.",
        ans: 'George Bush',
        correct: 1
        },
        {
        num: 3,
        quote: "They misunderestimated me.",
        ans : 'George Bush',
        correct: 1
        },
        {
        num: 4,
        quote: "I don't think there's ever been anyone like me that's lasted. And I'm going to keep lasting.",
        ans : 'Paris Hilton',
        correct: 0
        }
    ];

//DISPLAY CURRENT QUESTION//
     displayCurrentQuestion = function (qnum, question){
        var currentQuestionData = question[qnum];
        var userAnswer = $("input[type='radio']:checked").index("input[type='radio']");
        $('#quest_num').text(currentQuestionData.num);
        $('#quote q').text(currentQuestionData.quote);
    };

        var currentQuestionData = question[qnum];
        var userAnswer = $("input[type='radio']:checked").index("input[type='radio']");

//EVENT HANDLER FOR SUBMIT BUTTON
        $('#submit').on('click', function(qnum, question){ 

                if ( userAnswer == currentQuestionData.correct){
                    alert("Good job!");
                    correctAnswer++;
                    $("#correct").text(correctAnswer);
                } else {
                    alert("Youre wrong.");
                    wrongAnswer++;
                    $("#incorrect").text(wrongAnswer);
                }
            qnum++;
            displayCurrentQuestion(qnum, question);

        });
});

Your handler is wrong 您的处理程序有误

$('#submit').on('click', function(qnum, question){ 

This forces qnum to be the click event, because you are specifying it as a function argument. 这将强制qnum成为click事件,因为您将其指定为函数参数。 What you are looking for is already accessible in this scope, so try changing it to: 您正在寻找的内容已经可以在此范围内访问,因此请尝试将其更改为:

$('#submit').on('click', function(){ 

Converting comment to answer 将评论转换为答案

This is wrong 这是错的

 $('#submit').on('click', function(qnum, question){ 

should be 应该

 $('#submit').on('click', function(e){ 
   e.preventDefault(); // cancel submission

I fixed your code but you really need to learn about variable scope, for example: 我修复了您的代码,但是您确实需要了解变量范围,例如:

 displayCurrentQuestion = function (){//question, qnum are global
    currentQuestionData = question[qnum]; // currentQuestionData is also global but you were declaring it local so it wasn't updating properly
    $('#quest_num').text(currentQuestionData.num);
    $('#quote q').text(currentQuestionData.quote);
};

WORKING DEMO 工作演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:无法读取未定义的属性“未定义” - Uncaught TypeError: Cannot read property 'undefined' of undefined TypeError:无法读取未定义的属性“NaN” - TypeError: Cannot read property 'NaN' of undefined 谷歌热图 API - visualization_impl.js:2 Uncaught (in promise) TypeError: Cannot read property 'NaN' of undefined - google heatmap API - visualization_impl.js:2 Uncaught (in promise) TypeError: Cannot read property 'NaN' of undefined 未捕获的TypeError:无法读取未定义的属性“数量” - Uncaught TypeError: Cannot read property 'quantity' of undefined 未捕获的TypeError:无法读取未定义的属性'fromJSON' - Uncaught TypeError: Cannot read property 'fromJSON' of undefined 未捕获的TypeError:无法读取未定义的属性“ timing” - Uncaught TypeError: Cannot read property 'timing' of undefined 未捕获的TypeError:无法读取未定义的属性'formatter' - Uncaught TypeError: Cannot read property 'formatter' of undefined Uncaught TypeError:无法读取未定义的属性“ stopVideo” - Uncaught TypeError: Cannot read property 'stopVideo' of undefined 未捕获的类型错误:无法读取未定义的属性“setCrossOrigin” - Uncaught TypeError: Cannot read property 'setCrossOrigin' of undefined 未捕获的TypeError:无法读取未定义的属性'getAttribute' - Uncaught TypeError: Cannot read property 'getAttribute' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM