我使用jquery datepicker (v1.8.20),日期格式设置为'D MM d‘,类似于:'Wed 9.12’。当我从日历中选择与当前年份不同的年份时,getDate方法将返回当前年份。此外,当我再次打开日历时,选定的日期和月份都会保留,但会更改当前日期的年份。通常,当日期格式不包含year时,它看起来像是被设置为当前年。你知道怎么解决这个问题吗?
发布于 2012-11-27 23:34:02
Datepicker只在输入元素中存储值,所以如果你没有格式字符串中的年份,datepicker只是不存储它。jquery.ui开发人员之一的Here说,这不是一个错误,“日期选择器只被设计为选择一个完整的日期”。
无论如何,我在我的项目中遇到了同样的问题,并通过丑陋的技巧解决了它,这迫使datepicker存储完整的日期:
(function($) {
$.datepicker._selectDateParent = $.datepicker._selectDate;
$.datepicker._adjustInstDateParent = $.datepicker._adjustInstDate;
$.datepicker._adjustInstDate = function(inst, offset, period) {
var fullDate = inst.input.data('fullDate');
if(fullDate && !period) {
inst.drawYear = inst.currentYear = fullDate.getFullYear();
}
this._adjustInstDateParent(inst, offset, period);
};
$.datepicker._selectDate = function(id, dateStr) {
var target = $(id);
var inst = this._getInst(target[0]);
var date = this._daylightSavingAdjust(new Date(inst.currentYear, inst.currentMonth, inst.currentDay));
inst.input.data('fullDate', date);
this._selectDateParent(id, dateStr);
}
})(jQuery);我只用datepicker 1.9.0+进行了测试,我不确定这是不是一个好的解决方案,但它对我来说是有效的:)
https://stackoverflow.com/questions/12390305
复制相似问题