我想从日期选择器中禁用某些天。到目前为止,我能够禁用星期日和星期一,以及1月和2月的几个月。现在我一直试图禁用特定的日期
2014/11 / 27,2014 / 12 / 24,2014 / 12/24
我尝试过的一切都没有奏效。我对这一切都很陌生,并且不知道我做错了什么。
这是我到目前为止的代码
jQuery(document).ready(function($) {
$.datepicker.setDefaults({
dateFormat : 'yy/mm/dd',
beforeShowDay: disableSpecificWeekDays,
changeMonth: true,
changeYear: true,
firstDay: 0
});
var daysToDisable = [0, 1];
var monthsToDisable = [0, 1];
function disableSpecificWeekDays(date) {
var day = date.getDay();
if ($.inArray(day, daysToDisable) != -1) {
return [false];
}
var month = date.getMonth();
if ($.inArray(month, monthsToDisable) != -1) {
return [false];
}
return [true];
}
});
我想知道你是否可以禁用某个日期范围的星期一和星期日,比如从2014年9月1日到2014年5月31日。
非常感谢对此的任何帮助
更新9/26/14
感谢TimSPQR,如果已经弄明白的话。这是我对任何对此感兴趣的人的代码
jQuery(document).ready(function($) {
$.datepicker.setDefaults({
dateFormat : 'yy/mm/dd',
beforeShowDay: unavailable,
changeMonth: true,
changeYear: true,
firstDay: 0
});
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
var unavailableDates = ["2014/11/27","2014/12/25","2014/12/24"]; // yyyy/MM/dd
var unavailableDays = ["Monday","Sunday"];
var unavailableMonths = ["January", "February"]
var lowrange = new Date('5/25/2015');
var highrange = new Date('9/7/2015');
var lowrange2 = new Date('5/25/2016');
var highrange2 = new Date('9/7/2016');
function unavailable(date) {
ymd = date.getFullYear() + "/" + ("0"+(date.getMonth()+1)).slice(-2) + "/" + ("0"+date.getDate()).slice(-2);
day = new Date(ymd).getDay();
month = new Date(ymd).getMonth();
if (
((lowrange < date && date < highrange) && date.getDay() === 0) ||
((lowrange < date && date < highrange) && date.getDay() == 1) ||
((lowrange2 < date && date < highrange2) && date.getDay() === 0) ||
((lowrange2 < date && date < highrange2) && date.getDay() == 1) ||
($.inArray(ymd, unavailableDates) < 0 && $.inArray(days[day], unavailableDays) < 0 && $.inArray(months[month], unavailableMonths) < 0)
)
{
return [true, "enabled", "Book Now"];
} else {
return [false,"disabled","Closed"];
}
}
});
答案 0 :(得分:0)
以下是针对特定日期和周日周日FIDDLE的解决方案。
将您的排除日期放在数组中。
JS
var enabledDays = ['9/24/2014','9/26/2014','9/29/2014','10/25/2014'];
$(".mydate").datepicker({
beforeShowDay: disabledAll,
beforeShow: function (textbox, instance) {
instance.dpDiv.css({
marginTop: 2,
marginLeft: 1
});
}
});
function disabledAll(date)
{
var m = date.getMonth() + 1, d = date.getDate(), y = date.getFullYear();
for ( var i=0; i < enabledDays.length; i++ )
{
if( $.inArray( m + '/' + d + '/' + y, enabledDays) != -1 )
{
return [0];
}
}
return [1];
}
编辑:这是一个FIDDLE,可以在没有for循环的情况下执行您想要的操作。
编辑:源自HERE。