首页 > 解决方案 > 使用 jQuery UI 将特定日期变灰不起作用

问题描述

使用下面的代码,我试图将 21/12/2020 显示为灰色,因为我们要到星期二(2020 年 12 月 22 日)才会发货。我在下面的代码中找不到什么问题。请指出代码中可能导致它无法工作的错误。

<script>
    jQuery(document).ready( function() {
        jQuery(function() {
            jQuery("#ship_date").datepicker( {
            var minDate;
                // get current date
                var d = new Date();
                var month = d.getMonth()+1;
                var day = d.getDate();
                var current_date = (month<10 ? '0' : '') + month + '/' + (day<10 ? '0' : '') + day + d.getFullYear() + '/'; 
    
                if (current_date < new Date(11, 22, 2020) {
                    minDate = new Date( 11, 22, 2020 );
                } else {
                    minDate = +1;
                }
                maxDate: '+2M',
                beforeShowDay: jQuery.datepicker.noWeekends
            } );
        });
    });
</script>

标签: javascriptjquerydatepickerfrontend

解决方案


根据 PKTG 的要求更新了答案:

<script>
      $( function() {
        var cuttoffdate = new Date(2020,11, 22);
        $( "#ship_date" ).datepicker(
        {
                    maxDate: '+2M',
                     beforeShowDay: function (date) {
                        show = true;
                        if (date.getDay() === 0 || date.getDay() === 6) { show = false; }
                        if(date<cuttoffdate) { show=false;};
                        var display = [show, '', (show) ? '' : 'Not available'];
                        return display;
                    }
                }
        );
      } );

</script>

推荐阅读