在jquery-ui datepicker中的“今天”按钮上添加事件侦听器

时间:2012-01-05 15:28:14

标签: jquery-ui jquery-ui-datepicker

我使用的是datepicker表单jQuery-ui-1.8.16。

我有以下代码:

Site.Calendar = function() {

    // Set default setting for all calendars
    jQuery.datepicker.setDefaults({
        showOn : 'both',
        buttonImageOnly : true,
        buttonText: '',
        changeMonth : true,
        changeYear : true,
        showOtherMonths : true,
        selectOtherMonths : true,
        showButtonPanel : true,
        dateFormat : "D, d M, yy",
        showAnim : "slideDown",
        onSelect: Site.Calendar.customiseTodayButton
    });
};

Site.Calendar.customiseTodayButton = function(dateText, inst) {
    console.log("hello");
};

我的customiseTodayButton函数仅在我选择实际日期时触发,而不是在今日按钮上触发。

有没有办法覆盖今天按钮在jQuery datepicker中的工作方式?

由于

3 个答案:

答案 0 :(得分:6)

单击今日按钮时没有标准事件。但是,看一下jquery.ui.datepicker.js代码,它似乎调用了$.datepicker._gotoToday。我将假设customizeTodayButton你试图改变它当前所做的行为(不是外观,外观将通过样式完成)。要改变现有行为,最好知道它现在的作用。所以,请记住,这是所用函数的当前代码:

/* Action for current link. */
_gotoToday: function(id) {
    var target = $(id);
    var inst = this._getInst(target[0]);
    if (this._get(inst, 'gotoCurrent') && inst.currentDay) {
        inst.selectedDay = inst.currentDay;
        inst.drawMonth = inst.selectedMonth = inst.currentMonth;
        inst.drawYear = inst.selectedYear = inst.currentYear;
    }
    else {
        var date = new Date();
        inst.selectedDay = date.getDate();
        inst.drawMonth = inst.selectedMonth = date.getMonth();
        inst.drawYear = inst.selectedYear = date.getFullYear();
    }
    this._notifyChange(inst);
    this._adjustDate(target);
},

要使用您自己的功能覆盖此功能,您需要将代码更新为以下内容:

Site.Calendar = function() {
    //override the existing _goToToday functionality
    $.datepicker._gotoTodayOriginal = $.datepicker._gotoToday;
    $.datepicker._gotoToday = function(id) {
        // now, call the original handler
        $.datepicker._gotoTodayOriginal.apply(this, [id]);
        // invoke selectDate to select the current date and close datepicker.
        $.datepicker._selectDate.apply(this, [id]);
    };

    // Set default setting for all calendars
    jQuery.datepicker.setDefaults({
        showOn: 'both',
        buttonImageOnly: true,
        buttonText: '',
        changeMonth: true,
        changeYear: true,
        showOtherMonths: true,
        selectOtherMonths: true,
        showButtonPanel: true,
        dateFormat: "D, d M, yy",
        showAnim: "slideDown"
    });
};

此外,这是您正在寻找的工作jsFiddle

答案 1 :(得分:4)

我在这里发现了以下内容: Today button in jQuery Datepicker doesn't work

jQuery.datepicker._gotoToday = function(id) {
        var target = jQuery(id);
        var inst = this._getInst(target[0]);
        if (this._get(inst, 'gotoCurrent') && inst.currentDay) {
                inst.selectedDay = inst.currentDay;
                inst.drawMonth = inst.selectedMonth = inst.currentMonth;
                inst.drawYear = inst.selectedYear = inst.currentYear;
        }
        else {
                var date = new Date();
                inst.selectedDay = date.getDate();
                inst.drawMonth = inst.selectedMonth = date.getMonth();
                inst.drawYear = inst.selectedYear = date.getFullYear();
                this._setDateDatepicker(target, date);
                this._selectDate(id, this._getDateDatepicker(target));
        }
        this._notifyChange(inst);
        this._adjustDate(target);
    }

它只是重写goToToday方法并添加两行:

this._setDateDatepicker(target, date);
this._selectDate(id, this._getDateDatepicker(target));

也许有一种更清晰的方法可以用原来的答案Mark来解决这个问题?

答案 2 :(得分:3)

我以这种方式意识到了今天按钮的覆盖:

jQuery.datepicker._gotoToday = function(id) {
    var today = new Date();
    var dateRef = jQuery("<td><a>" + today.getDate() + "</a></td>");
    this._selectDay(id, today.getMonth(), today.getFullYear(), dateRef);
};

这很简单,我会选择“选择日期和关闭日期选择器”功能。