【问题标题】:jQuery Datepicker - Display only certain months (not a range)jQuery Datepicker - 仅显示特定月份(不是范围)
【发布时间】:2015-09-08 10:51:42
【问题描述】:

我试图强制我的 jQuery Datepicker 只允许选择 5 月和 10 月的月份,并根据用户是向前推进还是向后推进来增加或减少年份。

到目前为止,我使用以下代码越来越接近这个目标,但不知道用户是在 datepicker 的月份选择器上单击向前还是向后,我不知道是增加还是减少年份值。

$("#ApplicantExpectedStartDate").datepicker({
    onChangeMonthYear: function (year, month, inst) {
        // Force datepicker to display only May and October

        if ((month > 5) && (month < 10)) {
            $(this).datepicker("setDate", new Date(year, 9, 1));
            $(this).datepicker("refresh");
        }

        if (month > 10) {
            $(this).datepicker("setDate", new Date(year, 4, 1));
            $(this).datepicker("refresh");
        }
    },
    dateFormat: "dd/mm/yy",
    changeYear: true
});

我可以使用在 onChangeMonthYear 之前立即触发的任何事件来将新日期与旧日期进行比较以进行比较吗?有没有比我目前尝试的更直接的方法?

jsfiddle 在这里:http://jsfiddle.net/jfejhjs8/4/

【问题讨论】:

标签: javascript jquery datepicker


【解决方案1】:

这不是非常优雅,但这提供了我想要的结果。

$("#ApplicantExpectedStartDate").datepicker(
                    {
                        onChangeMonthYear: function (year, month, inst) {

                            // Force datepicker to display only May and October

                            // Navigating FROM May
                            if (month == 4) {
                                $(this).datepicker("setDate", new Date(year - 1, 9, 1));
                                $(this).datepicker("refresh");
                            }

                            if (month == 6) {
                                $(this).datepicker("setDate", new Date(year, 9, 1));
                                $(this).datepicker("refresh");
                            }

                            // Navigating FROM October
                            if (month == 9) {
                                $(this).datepicker("setDate", new Date(year, 4, 1));
                                $(this).datepicker("refresh");
                            }

                            if (month == 11) {
                                $(this).datepicker("setDate", new Date(year + 1, 4, 1));
                                $(this).datepicker("refresh");
                            }
                        },
                        dateFormat: "dd/mm/yy",
                        changeYear: true
                    }
            );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 2022-07-25
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多