Datepicker始终使用defaultDate而不是输入日期打开,格式为DAYS

时间:2013-10-28 16:02:00

标签: javascript jquery jquery-ui datepicker

我想引用一个我在网上用jqueryUI datepicker努力寻找的解决方案。

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

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

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

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

在1.9中使用,但也使用jQuery 1.10进行测试

1 个答案:

答案 0 :(得分:1)

完全归功于: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期间隐藏并显示日历。