开发者

jQuery object reference is empty

开发者 https://www.devze.com 2023-03-20 08:30 出处:网络
var quiz = { 开发者_如何学编程 config : { qType : $j(\'#questionTypeDdown\') }, data : { qType : \'test\'
var quiz = {

   开发者_如何学编程 config : {
        qType : $j('#questionTypeDdown')
    },

    data : {
        qType : 'test'
    },

    init: function() {
        quiz.assignUIActions();
    },

    assignUIActions : function() {

        var c = quiz.config;
        var d = quiz.data;

        quiz.assignqType(c,d);
    },

    assignqType : function(c,d) {
        console.log(c.qType);
        console.log(d.qType);
    }
};

$j(document).ready(function() { quiz.init(); });

console.log(c.qType) returns an empty jQuery object but

console.log(d.qType) returns the value 'test'

Please explain why this is happening and what is the right way to get reference to #questionTypeDdown.


You're running qType : $j('#questionTypeDdown') before the document is loaded.

You need to execute your object initializer in $j(document).ready.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号