0

这是我的代码

    var s;
var AddEvent =  {
    settings : {
        saveButton : $('#uploadfiles1'),
        cancelSpeech :  $('.cancelSpeech'),
        datePicker : $(".datepicker"),
        eventName : $('input[name=eventname]'),
        eventDate : $('input[name=eventdate]')
    },
    init:function(s){
        s = this.settings;
        this.BindEvents();
        $('.Wallpapers').addClass('active');
        $('input, textarea').placeholder();
    },
    BindEvents:function(){
        this.CancelButton();
        this.DatePicker();
       // this.SaveButton();
        $('input[type=text],textarea').on('keyup change',function(){
            AddEvent.FieldsCheck(); 
        });
    },
    CancelButton: function()
    {
        s.cancelSpeech.on('click',function(){
            var referrer =  document.referrer;
            window.location = referrer;
        });
    },
    DatePicker :function()
    {
        s.datePicker.datepicker({
            //defaultDate: +7,
            showOtherMonths: true,
            autoSize: true,
            //appendText: '(dd-mm-yyyy)',
            dateFormat: 'dd/mm/yy'
        });
    },
    SaveButton: function()
    {
        this.ClearFields();
    },
    FieldsCheck: function()
    {
        alert(s.eventName.attr('name'));
        if(s.eventName.val()!='' && s.eventDate.val() !='' && $('textarea').val()!='')
        {
            s.saveButton.removeAttr('disabled').removeClass('disabled');
        }
        else
            s.saveButton.attr('disabled','disabled').addClass('disabled');
    },
    ClearFields:function()
    {
        $('input,textarea').val('');
        this.FieldsCheck();
    }
};
$(function(){
    AddEvent.init(s);
});

我正在实现这个例子http://css-tricks.com/how-do-you-structure-javascript-the-module-pattern-edition/

但是每次当我在这一行输入我的输入字段时,我都会在警报中得到未定义

alert(s.eventName.attr('name'));

请告诉我我做错了什么我试图搜索但找不到任何有用的东西。

编辑:我在这里创建了一个小jsfiddle

TypeError: this.settings is undefined
[Break On This Error]   

console.log(this.settings.eventName.attr('id'));

谢谢

4

1 回答 1

2

你的问题在这里:

var s;
AddEvent.init(s);

这之后就没有办法了s定义。

一个解决方案是简单地不通过s(并且不在函数的参数中声明它):

init:function(s){
    s = this.settings;
...
AddEvent.init();

但这会让变量污染全局命名空间。

如果您希望您settings的所有功能都可以访问,您可以嵌入到一个闭包中:

var AddEvent =  (function(){
    var settings;
    return {
        init:function(){
                settings = {
                        saveButton : $('#uploadfiles1'),
                        cancelSpeech :  $('.cancelSpeech'),
                        datePicker : $(".datepicker"),
                        eventName : $('input[name=eventname]'),
                        eventDate : $('input[name=eventdate]')
                };
            this.BindEvents();
            $('.Wallpapers').addClass('active');
            $('input, textarea').placeholder();
        },
        BindEvents:function(){
            this.CancelButton();
            this.DatePicker();
           // this.SaveButton();
            $('input[type=text],textarea').on('keyup change',function(){
                AddEvent.FieldsCheck(); 
            });
        },
        CancelButton: function()
        {
            settings.cancelSpeech.on('click',function(){
                var referrer =  document.referrer;
                window.location = referrer;
            });
        },
        DatePicker :function()
        {
            settings.datePicker.datepicker({
                //defaultDate: +7,
                showOtherMonths: true,
                autoSize: true,
                //appendText: '(dd-mm-yyyy)',
                dateFormat: 'dd/mm/yy'
            });
        },
        SaveButton: function()
        {
            this.ClearFields();
        },
        FieldsCheck: function()
        {
            alert(settings.eventName.attr('name'));
            if(settings.eventName.val()!='' && settings.eventDate.val() !='' && $('textarea').val()!='')
            {
                settings.saveButton.removeAttr('disabled').removeClass('disabled');
            }
            else
                settings.saveButton.attr('disabled','disabled').addClass('disabled');
        },
        ClearFields:function()
        {
            $('input,textarea').val('');
            this.FieldsCheck();
        }
    }
})();

$(function(){
    AddEvent.init();
});
于 2013-04-15T08:41:31.290 回答