javascript 如何在 jQuery UI 日期选择器中禁用公共假期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22065143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i disable public holidays in a jQuery UI date picker?
提问by Jimmy_Chong
I'm making a holiday booking application and obviously you don't need to book off holidays that are already given to you, so I need to know how I can disable, Christmas, for example from the date picker every year without me having to change the code every year.
我正在制作假期预订应用程序,显然您不需要预订已经提供给您的假期,所以我需要知道如何禁用圣诞节,例如每年从日期选择器中禁用我不必每年更改代码。
This is my jQuery UI date picker code so far:
到目前为止,这是我的 jQuery UI 日期选择器代码:
<script>
$(function() {
$("#from").datepicker({
beforeShowDay: $.datepicker.noWeekends,
defaultDate: "today",
changeMonth: true,
numberOfMonths: 1,
minDate: "today",
dateFormat: "dd/mm/yy",
onClose: function(selectedDate) {
$( "#to" ).datepicker("option", "minDate", selectedDate);
}
});
$("#to").datepicker({
beforeShowDay: $.datepicker.noWeekends,
defaultDate: "today",
changeMonth: true,
numberOfMonths: 1,
minDate: "today",
dateFormat: "dd/mm/yy",
onClose: function(selectedDate) {
$("#from").datepicker("option", "maxDate", selectedDate);
}
});
});
</script>
回答by Anoop Joshi
you can exclude the dates like
你可以排除这样的日期
var holidays = ["2014-02-27","2014-02-01"];
$( "#from" ).datepicker({
beforeShowDay: function(date){
var datestring = jQuery.datepicker.formatDate('yy-mm-dd', date);
return [ holidays.indexOf(datestring) == -1 ]
}
});
you can provide more dates to the holidays array
您可以为假期数组提供更多日期
回答by Kapil Kshirsagar
You have to use beforeShowDay attribute of DatePicker like below:
您必须使用 DatePicker 的 beforeShowDay 属性,如下所示:
$("#textboxid").datepicker({
beforeShowDay: function(date) {
var day = date.getDay();
return [day != 0,''];
}
})?????;?
Above script will disable all Sundays.
以上脚本将禁用所有星期日。