2015-05-24 28 views
6

我有一個預訂酒店房間的表格,我在這裏有兩個名爲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); 
    } 
    }); 
}); 

回答

3

這撥弄應該幫助你,你只需要找出你想要禁用

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/

+0

謝謝你的回答,我有約會的日期s格式'2015-06-25 00:00:00',我們如何使用datepicker格式化它? –

+0

你正在使用日期選擇器,我不知道那裏是否有時間。你可以使用類似這樣的方式來修改日期格式'string.substring(10,「」)',它返回字符串的前10個字符 – VladNeacsu

+0

我們如何排除除這些日期以外的所有日期?相反的方式..... – Mikeys4u

相關問題