我已经在jQuery datepicker中禁用了所有周末。现在有一个要求,我必须启用几个周末约会。
我尝试将这些日期添加到数组中,然后启用它们,但是它不起作用。当前代码在下面,对于禁用周末来说效果很好,但是我无法启用所需的特定日期。任何帮助将不胜感激!
$(window).load(function() {
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
var availableDates = ["2018/12/22", "2018/12/23"]; // yyyy/MM/dd
var unavailableDates = ["2018/12/25", "2018/12/26", "2018/12/27"]; // yyyy/MM/dd
var unavailableDays = ["Saturday", "Sunday", "Monday"];
function unavailable(date) {
ymd = date.getFullYear() + "/" + ("0" + (date.getMonth() + 1)).slice(-2) + "/" + ("0" + date.getDate()).slice(-2);
day = new Date(ymd).getDay();
if ($.inArray(ymd, unavailableDates) < 0 && $.inArray(days[day], unavailableDays) < 0 && $.inArray(ymd, availableDates) < 1) {
return [true, "enabled", "Order now"];
} else {
return [false, "disabled", "Delivery not available on this date"];
}
}
$('#date').datepicker({
beforeShowDay: unavailable
});
});
答案 0 :(得分:0)
我想availableDates
必须优先于周末和unavailableDates
...
因此,如果有单独的条件。如果它的计算结果为true,则只需return
(这样就不会计算第二个条件)。
$(window).load(function() {
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
var availableDates = ["2018/12/22", "2018/12/23"]; // yyyy/MM/dd
var unavailableDates = ["2018/12/25", "2018/12/26", "2018/12/27"]; // yyyy/MM/dd
var unavailableDays = ["Saturday", "Sunday", "Monday"];
function unavailable(date) {
ymd = date.getFullYear() + "/" + ("0" + (date.getMonth() + 1)).slice(-2) + "/" + ("0" + date.getDate()).slice(-2);
day = new Date(ymd).getDay();
if($.inArray(ymd, availableDates) >= 0){ // Evaluate availableDates first
return [true, "enabled", "Order now"];
}
if ($.inArray(ymd, unavailableDates) < 0 && $.inArray(days[day], unavailableDays) < 0) {
return [true, "enabled", "Order now"];
} else {
return [false, "disabled", "Delivery not available on this date"];
}
}
$('#date').datepicker({
beforeShowDay: unavailable
});
});
答案 1 :(得分:0)
您需要稍微调整一下条件才能在可用和不可用的日期工作。将可用日期子句置于OR
条件中
function unavailable(date) {
ymd = date.getFullYear() + "/" + ("0" + (date.getMonth() + 1)).slice(-2) + "/" + ("0" + date.getDate()).slice(-2);
day = new Date(ymd).getDay();
if (($.inArray(ymd, unavailableDates) < 0 && $.inArray(days[day], unavailableDays) < 0)
|| $.inArray(ymd, availableDates) >= 0) {
return [true, "enabled", "Order now"];
} else {
return [false, "disabled", "Delivery not available on this date"];
}
}