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

发布于 2024-12-25 08:20:41 字数 766 浏览 3 评论 0原文

我正在使用日期选择器形式 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 日期选择器中“今天”按钮的工作方式吗?

谢谢

I'm using the datepicker form jQuery-ui-1.8.16.

I have the following code:

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");
};

My customiseTodayButton function is only getting triggered when I select an actual date and NOT on the Today button.

Is there any way to override how the today button work's in the jQuery datepicker?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无名指的心愿 2025-01-01 08:20:41

我发现这里发布了以下内容:
jQuery Datepicker 中的“今天”按钮不起作用

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));

也许有一种更干净的方法可以用您原来的答案马克来解决这个问题?

I found the following posted here:
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);
    }

It simply rewrites the goToToday method and adds two new lines:

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

Maybe there is a cleaner way to fix this with your original answer Mark?

贵在坚持 2025-01-01 08:20:41

单击“今天”按钮时没有标准事件。但是,查看 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 您正在寻找的内容。

There isn't a standard event for when the today button is clicked. However, taking a look at the jquery.ui.datepicker.js code, it appears that it calls $.datepicker._gotoToday. I'll assume by customizeTodayButton you're attempting to change the behavior of what it does currently (not the looks, the looks would be done with styling). To change the existing behavior, it's good to know what it does now. So, that in mind, this is the current code of the function used:

/* 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);
},

To override this function with your own functionality, you'll want to do update your code to something like this:

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"
    });
};

Also, here's a working jsFiddle of what you're looking for.

韶华倾负 2025-01-01 08:20:41

我以这种方式实现了 today 按钮的重写:

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);
};

这非常简单,并且执行了我想要的“选择日期并关闭日期选择器”功能。

I realized the overriding of the today button in this way:

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);
};

This is quite simple and does the "select date and close datepicker" functionality that I would.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文