Javascript 如何禁用已预订的日期?
我有一个预订酒店房间的表单,其中我有两个字段,分别是checkIn和checkOut。我在这里使用jQuery datepicker预订房间,我不想显示已经预订的日期。我试过这样做Javascript 如何禁用已预订的日期?,javascript,jquery,datepicker,jquery-ui-datepicker,Javascript,Jquery,Datepicker,Jquery Ui Datepicker,我有一个预订酒店房间的表单,其中我有两个字段,分别是checkIn和checkOut。我在这里使用jQuery datepicker预订房间,我不想显示已经预订的日期。我试过这样做 $(function() { var excludedCheckInDates = CHECKINDATES; // an array of already booked checkin dates var excludedCheckOutDates = CHECKOUTDATES; /
$(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);
}
});
});
$(函数(){
var excludedCheckInDates=CHECKINDATES;//已预订签入日期的数组
var excludedCheckOutDates=CHECKOUTDATES;//已预订的签出日期数组
$.datepicker
.setDefaults({
默认日期:“+1w”,
变化月:对,
变化年:是的,
minDate:0,
beforeShowDay:功能(日期){
日期=$.datepicker.formatDate('yy-mm-dd',日期);
excludedCheckInDates=$.INARAY(日期,
不包括胆碱酯酶)<0;
excludedCheckOutDates=$.inArray(日期,
不包括日期)<0;
如果(不包括支票){
返回[true,'selectedDate'];
}否则{
返回false;
}
如果(不包括检查日期){
返回true;
}否则{
返回false;
}
返回true;
}
});
$(“#签入”).datepicker({
onSelect:函数(selectedDate){
$(“#签入”).datepicker('option','minDate',
选择日期| | 0);
}
});
$(“#签出”).datepicker({
onSelect:函数(selectedDate){
$('#checkOut')。日期选择器('option','maxDate',selectedDate);
}
});
});
这个提琴应该可以帮助您,您只需要找出要禁用的日期数组
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 ]
}
});
下面是对我有用的更新JSFIDLE
谢谢您的回答,我正在使用这种格式的日期
2015-06-25 00:00:00
,我们如何使用datepicker设置格式?您使用的是datepicker,我不知道里面是否也有时间。您可以使用类似的方法来修剪日期格式string.substring(10,”)
,它返回字符串的前10个字符