JQuery datepicker:明确启用禁用日期
JQuery datepicker: Disable dates explicitly enabled
我有一个日期选择器可以过滤可选择的日期。要求只有 T、W、Th 和每月随机的一个星期六。到目前为止,我已经能够使用下面的代码完成此操作。现在,我的客户希望能够出于各种原因(例如假期、办公室关闭等)屏蔽 T、W、Th 的某些日子。我不确定如何完成这个新请求。有什么想法吗?
var SaturdayDate = ["7-25-2020", "8-15-2020" ];
$(function() {
$("#my_date_picker").datepicker({
beforeShowDay: function(dt) {
return [dt.getDay() == 2 || dt.getDay() == 3 || dt.getDay() == 4 || enableThisDay(dt), ""];
},
minDate: 1
});
});
function enableThisDay(date) {
var oDate = $.datepicker.formatDate('m-d-yy', date);
if ($.inArray(oDate, SaturdayDate) != -1) {
return [true];
}
}
Datepicker 接受需要禁用的日期数组。
您应该可以通过以下方式禁用日期:
var dates = ["20/07/2020", "21/07/2020", "22/07/2020", "23/07/2020"];
function disableDates(date) {
var string = jQuery.datepicker.formatDate('dd/mm/yy', date);
let isDefaultDisabled = false;
if(date.getDay()===2 || date.getDay()==3 || date.getDay()==4){
isDefaultDisabled = true;
}
return [ isDefaultDisabled && dates.indexOf(string) == -1 ];
}
$(function() {
$("#date").datepicker({
beforeShowDay: disableDates
});
});
我会保留任何数组以使用 Date
对象而不是字符串,从而消除格式化这一潜在问题。我有两个数组:“随机”星期六和假期。然后我会有一个函数来检查日期是否启用。
// Dates in JS use 0 indexed months, so 6 is July and 7 is August
var saturdays = [ new Date(2020, 6, 25), new Date(2020, 7, 15) ];
var holidays = [ new Date(2020, 6, 22), new Date(2020, 6, 2), new Date(2020, 7, 13) ];
$(function() {
$("#my_date_picker").datepicker({
beforeShowDay: enableThisDay,
minDate: 1
});
});
function enableThisDay(date) {
var enabled = false;
// if it's Tue, Wed, Thu, enable it
if ([2,3,4].includes(date.getDay())) {
enabled = true;
}
// if it's a holiday, disable it
if (holidays.some(h => h.valueOf() === date.valueOf())) {
enabled = false;
}
// if it's a saturday, disable it
// note that if it's a saturday, that overrides holiday
// if holidays override saturday, swap this statement with
// the one immediately above
if (saturdays.some(s => s.valueOf() === date.valueOf())) {
enabled = true;
}
return [enabled, ""];
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylehseet">
<link href="https://code.jquery.com/ui/1.12.1/themes/ui-lightness/jquery-ui.css" rel="stylehseet">
<input type="text" id="my_date_picker">
我有一个日期选择器可以过滤可选择的日期。要求只有 T、W、Th 和每月随机的一个星期六。到目前为止,我已经能够使用下面的代码完成此操作。现在,我的客户希望能够出于各种原因(例如假期、办公室关闭等)屏蔽 T、W、Th 的某些日子。我不确定如何完成这个新请求。有什么想法吗?
var SaturdayDate = ["7-25-2020", "8-15-2020" ];
$(function() {
$("#my_date_picker").datepicker({
beforeShowDay: function(dt) {
return [dt.getDay() == 2 || dt.getDay() == 3 || dt.getDay() == 4 || enableThisDay(dt), ""];
},
minDate: 1
});
});
function enableThisDay(date) {
var oDate = $.datepicker.formatDate('m-d-yy', date);
if ($.inArray(oDate, SaturdayDate) != -1) {
return [true];
}
}
Datepicker 接受需要禁用的日期数组。
您应该可以通过以下方式禁用日期:
var dates = ["20/07/2020", "21/07/2020", "22/07/2020", "23/07/2020"];
function disableDates(date) {
var string = jQuery.datepicker.formatDate('dd/mm/yy', date);
let isDefaultDisabled = false;
if(date.getDay()===2 || date.getDay()==3 || date.getDay()==4){
isDefaultDisabled = true;
}
return [ isDefaultDisabled && dates.indexOf(string) == -1 ];
}
$(function() {
$("#date").datepicker({
beforeShowDay: disableDates
});
});
我会保留任何数组以使用 Date
对象而不是字符串,从而消除格式化这一潜在问题。我有两个数组:“随机”星期六和假期。然后我会有一个函数来检查日期是否启用。
// Dates in JS use 0 indexed months, so 6 is July and 7 is August
var saturdays = [ new Date(2020, 6, 25), new Date(2020, 7, 15) ];
var holidays = [ new Date(2020, 6, 22), new Date(2020, 6, 2), new Date(2020, 7, 13) ];
$(function() {
$("#my_date_picker").datepicker({
beforeShowDay: enableThisDay,
minDate: 1
});
});
function enableThisDay(date) {
var enabled = false;
// if it's Tue, Wed, Thu, enable it
if ([2,3,4].includes(date.getDay())) {
enabled = true;
}
// if it's a holiday, disable it
if (holidays.some(h => h.valueOf() === date.valueOf())) {
enabled = false;
}
// if it's a saturday, disable it
// note that if it's a saturday, that overrides holiday
// if holidays override saturday, swap this statement with
// the one immediately above
if (saturdays.some(s => s.valueOf() === date.valueOf())) {
enabled = true;
}
return [enabled, ""];
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylehseet">
<link href="https://code.jquery.com/ui/1.12.1/themes/ui-lightness/jquery-ui.css" rel="stylehseet">
<input type="text" id="my_date_picker">