【发布时间】:2014-05-11 10:35:47
【问题描述】:
我有一个问题,与year/month only datepicker inline 的问题非常相似
不同之处在于我使用的是input 版本,而不是div。
在div 的情况下,ui-datepicker-calendar 在外部 div 内,因此可以唯一访问。
但是对于input,输入和ui-datepicker-calendar 之间没有关系(或者,不是我发现的......)
为了清楚起见,我不能使用“全局”.ui-datepicker-calendar { display: none;},因为我还有其他完整的日期选择器(即也使用日期)。
我的代码如下所示:
$("#monthsYearInput").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: "mm/yy",
onClose: function(dateText) {
if (dateText != "") {
var selectedDate = $.datepicker.parseDate("dd/mm/yy", "01/" + dateText);
$(this).datepicker("setDate", selectedDate);
$(this).datepicker("refresh");
}
},
beforeShow: function() {
var dateText = $(this).val();
var isPopulated = dateText.length > 0;
if (isPopulated) {
var selectedDate = $.datepicker.parseDate("dd/mm/yy", "01/" + dateText);
$(this).datepicker("option", "defaultDate", selectedDate);
$(this).datepicker("setDate", selectedDate);
$(this).datepicker("refresh");
}
},
onChangeMonthYear: function(year, month) {
if (changingDate) {
return;
}
changingDate = true;
$(this).datepicker("setDate", new Date(year, month - 1, 1));
$(this).datepicker("refresh");
changingDate = false;
}
});
$("#monthsYearInput").focus(function () {
$(".ui-datepicker-calendar").hide();
};
$("#monthsYearInput").blur(function () {
$(".ui-datepicker-calendar").hide();
};
开始时看起来一切正常,但是当年/月发生变化时,新日期会填充到输入中(需要),但现在会显示日历(不需要)。
关于如何解决这个问题的任何想法?...
提前致谢。
JSfiddle:http://jsfiddle.net/OhadRaz/8z9M2/
【问题讨论】:
-
如果日期的值没有提供给Date constructor,则默认设置为1。
-
谢谢你。但这与所描述的问题有什么关系?...
-
没什么,这是对代码的评论,不是答案。这就是为什么它是一个评论。 :-)
标签: javascript jquery jquery-ui calendar datepicker