我试图强制我的 jQuery Datepicker 仅允许选择 5 月和 10 月,并根据用户是向前推进还是向后推进来增加或减少年份。
到目前为止,我通过以下代码越来越接近这个目标,但不知道用户是在日期选择器的月份选择器上向前还是向后单击,我不知道是否增加或减少年份值。
$("#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/
最佳答案
这不是非常优雅,但这提供了我想要的结果。
$("#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
}
);
关于javascript - jQuery Datepicker - 仅显示某些月份(不是范围),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32455838/