【问题标题】:how to disable already booked dates?如何禁用已预订的日期?
【发布时间】:2015-08-06 04:36:09
【问题描述】:

我有一个预订酒店房间的表格,其中有两个字段,称为 checkIn 和 checkOut。我在这里使用 jQuery datepicker 预订房间我不想显示那些已经预订的日期。我试过这样。

$(function() {
  var excludedCheckInDates = CHECKINDATES; // an array of already booked checkin dates           
  var excludedCheckOutDates = CHECKOUTDATES; // an array of already booked checkout dates
  $.datepicker
    .setDefaults({
      defaultDate: '+1w',
      changeMonth: true,
      changeYear: true,
      minDate: 0,
      beforeShowDay: function(date) {
        date = $.datepicker.formatDate('yy-mm-dd', date);
        excludedCheckInDates = $.inArray(date,
          excludedCheckInDates) < 0;
        excludedCheckOutDates = $.inArray(date,
          excludedCheckOutDates) < 0;
        if (excludedCheckInDates) {
          return [true, 'selectedDate'];
        } else {
          return false;
        }
        if (excludedCheckOutDates) {
          return true;
        } else {
          return false;
        }
        return true;
      }
    });
  $('#checkIn').datepicker({
    onSelect: function(selectedDate) {
      $('#checkIn').datepicker('option', 'minDate',
        selectedDate || 0);
    }
  });
  $('#checkOut').datepicker({
    onSelect: function(selectedDate) {
      $('#checkOut').datepicker('option', 'maxDate', selectedDate);
    }
  });
});

【问题讨论】:

    标签: javascript jquery datepicker jquery-ui-datepicker


    【解决方案1】:

    这个小提琴应该可以帮助你,你只需要找出你想要禁用的日期数组

     var array = ["2015-06-14","2015-06-15","2015-06-16"]
    
    $('input').datepicker({
      beforeShowDay: function(date){
        var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
        return [ array.indexOf(string) == -1 ]
      }
    });
    

    http://jsfiddle.net/CxNNh/2201/

    这是适用于我的更新后的 jsfiddle

    http://jsfiddle.net/CxNNh/2202/

    【讨论】:

    • 感谢您的回答,我有这种格式的日期2015-06-25 00:00:00,我们如何使用 datepicker 格式化它?
    • 你正在使用 datepicker,我不知道那里是否也有时间。您可以使用类似这样的东西来修剪您的日期格式string.substring(10, ""),它会返回字符串的前 10 个字符
    猜你喜欢
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多