日期选择器始终以默认日期打开,而不是以没有 DAYS 的格式输入日期

Datepicker always opening with defaultDate instead of input date with format without DAYS

本文关键字:日期 输入 DAYS 格式 选择器 默认      更新时间:2023-09-26

我想引用一个我在网上很难找到的解决方案 jqueryUI 日期选择器。

使用这些选项创建日期选取器时(假设 fromDate 和 toDate 是 2 个日期对象):

{
    changeMonth: true,
    changeYear: true,
    minDate: fromDate,
    maxDate: toDate,
    defaultDate: toDate,
    dateFormat: 'MM yy'
}

每当您打开DP时,您都会获得默认日期,而不是先前选择的日期。

我尝试使用 OnClose 设置日期事件,该事件将正确设置内部日期和输入日期,但不能正确设置select option值。这里的例子:http://jsfiddle.net/techunter/Y8MZ4/

在 1.9 中小提琴,但也用 jQuery 1.10 进行了测试

全部功劳归于: http://jquerybyexample.blogspot.com/2012/06/show-only-month-and-year-in-jquery-ui.html

这里没有魔法,您需要手动更新选择选项。

beforeShow: function () {
        if ((selDate = $(this).val()).length > 0)
        {
            iYear = selDate.substring(selDate.length - 4, selDate.length);
            iMonth = jQuery.inArray(selDate.substring(0, selDate.length - 5),
            $(this).datepicker('option', 'monthNames'));
            $(this).datepicker('option', 'defaultDate', new Date(iYear, iMonth, 1));
            $(this).datepicker('setDate', new Date(iYear, iMonth, 1));
        }
    }

顺便说一句,我更喜欢在beforeShowonClose期间隐藏和显示日日历。