在jquery-ui datapicker中的"今天"按钮上添加事件侦听器

Tho*_*ley 4 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");
};
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢

Pri*_*ark 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);
},
Run Code Online (Sandbox Code Playgroud)

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

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"
    });
};
Run Code Online (Sandbox Code Playgroud)

此外,这是一个有用的jsFiddle,你正在寻找什么.


Tho*_*ley 5

我在这里找到了以下内容: jQuery Datepicker中的Today按钮今天不起作用

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);
    }
Run Code Online (Sandbox Code Playgroud)

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

this._setDateDatepicker(target, date);
this._selectDate(id, this._getDateDatepicker(target));
Run Code Online (Sandbox Code Playgroud)

也许有一种更干净的方法可以用您原来的答案标记解决此问题?