There is the beforeShowDay
option, which takes a function to be called for each date, returning true if the date is allowed or false if it is not.(有一个beforeShowDay
选项,该选项需要为每个日期调用一个函数,如果允许日期,则返回true,否则,则返回false。)
From the docs:(从文档:)
beforeShowDay(ShowShow之前)
The function takes a date as a parameter and must return an array with [0] equal to true/false indicating whether or not this date is selectable and 1 equal to a CSS class name(s) or '' for the default presentation.(该函数将日期作为参数,并且必须返回一个数组,该数组的[0]等于true / false,表示此日期是否可选,默认表示形式为1,该数组等于CSS类名或。) It is called for each day in the datepicker before is it displayed.(在日期选择器中的每一天都会调用它,然后再显示它。)
Display some national holidays in the datepicker.(在日期选择器中显示一些国定假日。)
$(".selector").datepicker({ beforeShowDay: nationalDays})
natDays = [
[1, 26, 'au'], [2, 6, 'nz'], [3, 17, 'ie'],
[4, 27, 'za'], [5, 25, 'ar'], [6, 6, 'se'],
[7, 4, 'us'], [8, 17, 'id'], [9, 7, 'br'],
[10, 1, 'cn'], [11, 22, 'lb'], [12, 12, 'ke']
];
function nationalDays(date) {
for (i = 0; i < natDays.length; i++) {
if (date.getMonth() == natDays[i][0] - 1
&& date.getDate() == natDays[i][1]) {
return [false, natDays[i][2] + '_day'];
}
}
return [true, ''];
}
One built in function exists, called noWeekends, that prevents the selection of weekend days.(存在一个内置函数,称为noWeekends,它可以防止选择周末。)
$(".selector").datepicker({ beforeShowDay: $.datepicker.noWeekends })
To combine the two, you could do something like (assuming the nationalDays
function from above):(要将两者结合起来,您可以执行类似的操作(假设上面的nationalDays
函数):)
$(".selector").datepicker({ beforeShowDay: noWeekendsOrHolidays})
function noWeekendsOrHolidays(date) {
var noWeekend = $.datepicker.noWeekends(date);
if (noWeekend[0]) {
return nationalDays(date);
} else {
return noWeekend;
}
}
Update : Note that as of jQuery UI 1.8.19, the beforeShowDay option also accepts an optional third paremeter, a popup tooltip(更新 :请注意,从jQuery UI 1.8.19开始, beforeShowDay选项还接受可选的第三个参数,即弹出工具提示) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…