jQuery Datepicker - 如何在加载时调用函数

时间:2012-11-30 19:41:04

标签: jquery jquery-ui jquery-ui-datepicker

我有一个加载事件的日期选择器。当月份更改为另一个月时,将进行数据库调用以恢复事件&这很好用。但是,我还需要在首次显示日历时以及稍后使用用户交互时进行此操作。这是我的代码,为了简化我的问题,如何在日历首次显示在屏幕上时运行“getJsonDate”函数?我几乎可以肯定这是一个相当基本的东西,但它不会向我跳出来。

       $(function () {
        function getJsonDate(year, month) {
            var theYear = year;
            var theMonth = month;
            alert('You are in, baby!');
            $.post('GetMonthCalendar', {
                data: { year: theYear, month: theMonth },
                success: function(data) {
                    //TODO: make determination on ResourceScheduleTypeId for correct css
                    var i = 0;
                    for (i = 0; i < data.data.length; i++) {
                        $('.ui-datepicker-calendar td a:exactly(' + data.data[i]['d'] + ')')
                            .css({ color: 'blue' })
                            .attr('href', data.data[i]['link'])
                            .parent().attr('onclick', '');
                    }

                }
            });
        }


        $.expr[":"].exactly = function (el, i, m) {
            var s = m[3];
            if (!s) return false;
            return eval("/^" + s + "$/i").test($(el).text());
        };
        $('#date').datepicker({
            inline: true,
            onSelect: function (dateText, inst) {
                Date.prototype.toString = function () { return isNaN(this) ? 'NaN' : [this.getDate(), this.getMonth(), this.getFullYear()].join('/'); };
                d = new Date(dateText);
                //alert(d.getDate() + ", " + d.getFullYear());
                getJsonDate(d.getFullYear(), d.getDate());
            },
            onChangeMonthYear: function (year, month, inst) {
                //alert(year + ", " + month);
                getJsonDate(year, month);
            }
        });
    });

1 个答案:

答案 0 :(得分:0)

由于datepicker继承自Widget,我认为您可以添加一个create callback,该窗口将在窗口小部件初始化后执行。

$("#date").datepicker({
    // …
    create: function(event, ui) {
        getJsonDate(/* year, month */)
    }
})