beforeShowDay
要在显示月份时限制范围,如下所示:
var d1s = new Date(2010, 8, 1),
d1e = new Date(2010, 8, 20),
d2s = new Date(2010, 9, 1),
d2e = new Date(2010, 9, 20);
$(function() {
$("#datepicker").datepicker({
numberOfMonths: 1,
beforeShowDay: function(date) {
return [(date >= d1s && date <= d1e || date >= d2s && date <= d2e), ''];
},
minDate: d1s,
maxDate: d2e
});
});â
You can give it a try here
或者,对于任意数量的日期范围,这里有一种效率稍低但更灵活的方法:
var ranges = [ { start: new Date(2010, 8, 1), end: new Date(2010, 8, 20) },
{ start: new Date(2010, 9, 1), end: new Date(2010, 9, 20) },
{ start: new Date(2010, 10, 1), end: new Date(2010, 10, 20) } ];
$(function() {
$("#datepicker").datepicker({
numberOfMonths: 1,
beforeShowDay: function(date) {
for(var i=0; i<ranges.length; i++) {
if(date >= ranges[i].start && date <= ranges[i].end) return [true, ''];
}
return [false, ''];
},
minDate: ranges[0].start,
maxDate: ranges[ranges.length -1].end
});
});â
You can give this version a try here
,只需按时间顺序排列范围:)