是否可以使jQuery UI Datepicker禁用星期六和星期日(和假期)? [英] Can the jQuery UI Datepicker be made to disable Saturdays and Sundays (and holidays)?
问题描述
我使用日期选择器来选择预约日。我已经将日期范围设置为仅适用于下个月。这很好。我想从可用的选择中排除星期六和星期日。可以这样做吗?如果是这样,怎么样?
I use a datepicker for choosing an appointment day. I already set the date range to be only for the next month. That works fine. I want to exclude Saturdays and Sundays from the available choices. Can this be done? If so, how?
推荐答案
有 beforeShowDay
选项,为每个日期调用一个函数,如果允许日期则返回true,否则返回false。来自文档:
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. From the docs:
beforeShowDay
该函数将日期作为参数,并且必须返回一个数组,其中[0]等于true / false,表示此日期是否可选,并且 1 等于CSS类名称或用于默认演示文稿。在显示日期选择器之前,它会被调用。
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. 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, ''];
}
存在一个内置函数,名为noWeekends,阻止周末日的选择。
One built in function exists, called noWeekends, that prevents the selection of weekend days.
$(".selector").datepicker({ beforeShowDay: $.datepicker.noWeekends })
要合并两者,你可以做一些事情(假设 nationalDays
上面的函数):
$(".selector").datepicker({ beforeShowDay: noWeekendsOrHolidays})
function noWeekendsOrHolidays(date) {
var noWeekend = $.datepicker.noWeekends(date);
if (noWeekend[0]) {
return nationalDays(date);
} else {
return noWeekend;
}
}
更新:请注意从jQuery UI 1.8.19开始, beforeShowDay选项也接受可选的第三个参数,弹出工具提示
Update: Note that as of jQuery UI 1.8.19, the beforeShowDay option also accepts an optional third paremeter, a popup tooltip
这篇关于是否可以使jQuery UI Datepicker禁用星期六和星期日(和假期)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!