如何在 jquery UI 日期选择器中启用禁用日期?

How to enable a disabled date in jquery UI datepicker?

我的购物车上有一个交货日期输入字段。我在日期选择器中禁用了周末和一些 public 假期。但无法启用某些禁用日期。我想启用 12 月 23 日和 24 日。

我的代码

var disableddates = ["1-1-2017","1-2-2017","1-26-2017","3-13-2017","4-14-2017","4-15-2017","4-16-2017","4-17-2017","4-25-2017","6-12-2017","9-25-2017","11-27-2017","11-28-2017","12-25-2017","12-26-2017","1-1-2018","1-2-2018","1-3-2018","1-4-2018","1-5-2018","1-8-2018","1-9-2018","1-10-2018","1-11-2018","1-12-2018","1-26-2018","3-12-2018","3-30-2018","3-31-2018","4-1-2018","4-2-2018","4-25-2018","6-11-2018","11-6-2018","12-25-2018","12-26-2018"];
function DisableSpecificDates(date) {

 var m = date.getMonth();
 var d = date.getDate();
 var y = date.getFullYear();

 // First convert the date in to the mm-dd-yyyy format 
 // Take note that we will increment the month count by 1 
 var currentdate = (m + 1) + '-' + d + '-' + y ;



 // We will now check if the date belongs to disableddates array 
 for (var i = 0; i < disableddates.length; i++) {

 // Now check if the current date is in disabled dates array. 
 if ($.inArray(currentdate, disableddates) != -1 ) {
 return [false];
 } 
 }

 // In case the date is not present in disabled array, we will now check if it is a weekend. 
 // We will use the noWeekends function
 var weekenddate = $.datepicker.noWeekends(date);
 return weekenddate; 

}
$("#delivery-date").datepicker({
        minDate: disablerange,
        beforeShowDay: DisableSpecificDates,
        dateFormat: "dd/mm/yy"
      });

您应该知道,当您使用 $.inArray() 函数时,您不再需要遍历所有值。回到问题,你只需要添加一个检查,你现在可以强制启用一些日期。

var disableddates = ["1-1-2017", "1-2-2017", "1-26-2017", "3-13-2017", "4-14-2017", "4-15-2017", "4-16-2017", "4-17-2017", "4-25-2017", "6-12-2017", "9-25-2017", "11-27-2017", "11-28-2017", "12-25-2017", "12-26-2017", "1-1-2018", "1-2-2018", "1-3-2018", "1-4-2018", "1-5-2018", "1-8-2018", "1-9-2018", "1-10-2018", "1-11-2018", "1-12-2018", "1-26-2018", "3-12-2018", "3-30-2018", "3-31-2018", "4-1-2018", "4-2-2018", "4-25-2018", "6-11-2018", "11-6-2018", "12-25-2018", "12-26-2018"];
var enableddates = ["12-23-2017", "12-24-2017"];

function DisableSpecificDates(date) {

    var m = date.getMonth();
    var d = date.getDate();
    var y = date.getFullYear();

    // First convert the date in to the mm-dd-yyyy format 
    // Take note that we will increment the month count by 1 
    var currentdate = (m + 1) + '-' + d + '-' + y;

    // Now check if the current date is in enabled dates array. 
    if ($.inArray(currentdate, enableddates) != -1) {
        return [true];
    }

    // Now check if the current date is in disabled dates array. 
    if ($.inArray(currentdate, disableddates) != -1) {
        return [false];
    }

    // In case the date is not present in disabled array, we will now check if it is a weekend. 
    // We will use the noWeekends function
    var weekenddate = $.datepicker.noWeekends(date);
    return weekenddate;

}
$("#delivery-date").datepicker({
    minDate: disablerange,
    beforeShowDay: DisableSpecificDates,
    dateFormat: "dd/mm/yy"
});