Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

javascript - 可以使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?(如果是这样,怎么办?)   ask by cletus translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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选项还接受可选的第三个参数,即弹出工具提示)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...