【问题标题】:jQuery UI Datepicker - Disable Holidays and Saturdays - Add FridayjQuery UI Datepicker - 禁用假期和星期六 - 添加星期五
【发布时间】:2021-03-12 13:29:42
【问题描述】:

我的 Datepicker 目前禁用节假日和周末。我正在尝试将星期五添加到脚本中

var holidays = <?php echo $holidays; ?>;
// var holidays =[ [2020,01,01,'New Years Day'],[2020,04,21,'My Birthday'],[2020,12,25,'Christmas Day'] ];

function setHolidays(date) {
    for (i = 0; i < holidays.length; i++) {
        if (date.getFullYear() == holidays[i][0]
            && date.getMonth() == holidays[i][1] - 1
            && date.getDate() == holidays[i][2]) {
            return [false, 'holiday', holidays[i][3]];
        }
    }
    var noWeekend = $.datepicker.noWeekends(date);
    return !noWeekend[0] ? noWeekend : [true];
}

$( ".add_delivery_date").datepicker( {          
    firstDay: 0,
    beforeShowDay: setHolidays
});

【问题讨论】:

    标签: jquery jquery-ui jquery-ui-datepicker


    【解决方案1】:

    您可以在setHolidays 函数中添加另一个检查来检查星期五的日期。

    我在假期数组的循环之后添加了检查:

    //var holidays = <?php echo $holidays; ?>;
     var holidays =[ [2020,01,01,'New Years Day'],[2020,04,21,'My Birthday'],[2020,12,25,'Christmas Day'] ];
    
    function setHolidays(date) {
        for (i = 0; i < holidays.length; i++) {
            if (date.getFullYear() == holidays[i][0]
                && date.getMonth() == holidays[i][1] - 1
                && date.getDate() == holidays[i][2]) {
                return [false, 'holiday', holidays[i][3]];
            }
        }
    
        if (date.getDay() === 5) { // No Friday's
            return [false, '', ''];
        }
    
        var noWeekend = $.datepicker.noWeekends(date);
        return !noWeekend[0] ? noWeekend : [true];
    }
    
    $(document).ready(function () {
      $( ".add_delivery_date").datepicker( {          
          firstDay: 0,
          beforeShowDay: setHolidays
      });
    });
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    
    Date:  <input type="text" class="add_delivery_date"> <br />

    【讨论】:

      猜你喜欢
      • 2020-10-08
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多