datepicker beforeshowday返回不起作用

时间:2019-05-28 09:22:31

标签: jquery datepicker

我正在尝试使用beforeShowDay禁用并突出显示日历中的日期。我发现此Fiddle代码有效。当我返回false时,它可以工作并禁用那一天,但是我正尝试使用CSS类返回[false, 'holiday', ''],但是它不起作用或没有禁用日期。

        function add_zero(num){
        if(num<10){
            return '0'+num;
        }else{
            return num;
        }
    }
$('.date').datepicker({
  format: "mm-dd-yyyy",
  autoclose: true,
  daysOfWeekDisabled: [0],
  weekStart: [1],
  beforeShowDay: function(Date) {
    var array = <?php echo json_encode($arry); ?>;
    var curr_day = add_zero(Date.getDate());
    var curr_month = add_zero(Date.getMonth() + 1);
    var curr_year = Date.getFullYear();
    var curr_date = curr_month + '-' + curr_day + '-' + curr_year;

    for (i = 0; i < array.length; i++) {
      if (curr_date == array[i]) {
        // return false;
        return [false, 'holiday', ''];
      }
    }
    //return [true,''];
    return true;
  }
}).on('changeDate', cal_date);

1 个答案:

答案 0 :(得分:0)

大多数问题似乎都在您的php日期格式中。

jsfiddle Link

var highlight = ['2019/05/19', '2019/05/20'];
//tips are optional but good to have
var tips = ['some description', 'some other description'];
var disable_dates = ['2019/05/01', '2019/05/08', '2019/05/15', '2019/05/22', '2019/05/29']
$('.date').datepicker({

  format: "mm-dd-yyyy",
  autoclose: true,
  daysOfWeekDisabled: [0],
  weekStart: [1],
  beforeShowDay: function (date) {
        var datestring = jQuery.datepicker.formatDate('yy/mm/dd', date);
        var hindex = $.inArray(datestring, highlight);
        if (hindex > -1) {
            return [true, 'highlight yourcustomcss', tips[hindex]];
        }
        var aindex = $.inArray(datestring, disable_dates);
                if(aindex == -1)
                    return [true,'custom_enabled']
                else
          return [false,'custom_disabled']
    }
})