23

是否有任何(简单)方法可以将 jQuery UI Datepicker 设置为不允许选择特定的预定日期?

我能够使这种方法正常工作,但是,它会产生一个空错误,从而阻止它在 IE 中显示。

'natDays[...].0' 为 null 或不是对象

提前致谢!

更新:如果我包含一些代码可能会有所帮助,对吧?无论如何,大部分内容都是直接取自上述线程:

natDays = [
        [7, 23], [7, 24], [8, 13], [8, 14], 
    ];

    function nationalDays(date) {
        for (i = 0; i < natDays.length; i++) {
            if (date.getMonth() == natDays[i][0] - 1
            && date.getDate() == natDays[i][1]) {
                return [false, natDays[i][2] + '_day'];
            }
        }
        return [true, ''];
    }

    function noWeekendsOrHolidays(date) {
        var noWeekend = $.datepicker.noWeekends(date);
        if (noWeekend[0]) {
            return nationalDays(date);
        } else {
            return noWeekend;
        }
    }


    $(function() { 
        $("#datepicker").datepicker({
            inline: true,
            minDate: new Date(2009, 6, 6), 
            maxDate: new Date(2009, 7, 14), 
            numberOfMonths: 2, 
            hideIfNoPrevNext: true,
            beforeShowDay: $.datepicker.noWeekends,
            altField: '#alternate',
        }); 
    });

再次感谢!

4

6 回答 6

14

这是一种禁止选择特定日期的方法:

var unavailableDates = ["9-5-2011","14-5-2011","15-5-2011"];

function unavailable(date) {
  dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
  if ($.inArray(dmy, unavailableDates) < 0) {
    return [true,"","Book Now"];
  } else {
    return [false,"","Booked Out"];
  }
}

$('#iDate').datepicker({ beforeShowDay: unavailable });

来源:jQuery - Datepicker - 禁用特定日期

于 2011-05-06T06:36:40.253 回答
9

您是否尝试过在前面使用 'var' 关键字正确声明 natDays?

另外 - 您在 natDays 定义的末尾有一个额外的逗号。

natDays[i][2] 不起作用,因为您的数组中只有 2 个项目 - 试试 ''

此外,您需要正确设置 beforeShowDay 函数名称 - 看起来它甚至不会调用您的自定义函数

于 2009-03-25T17:21:42.333 回答
9

您可以使用该beforeShowDay选项。我需要禁用该月 28 日之后的任何一天。这是我的代码。

$('.datepicker').datepicker({
    dateFormat: "yy-mm-dd",
    beforeShowDay: function(date) {
        var day = date.getDate();
        if (day > 28) {
            return [false];
        } else {
            return [true];
        }
    }
});

以下是有关它的更多信息:http: //api.jqueryui.com/datepicker/#option-beforeShowDay

传递给 beforeShowDay 回调的日期变量是一个 JavaScript 日期对象,因此可以使用各种库对其进行格式化,可以使用 date.getTime() 等检索时间戳。

于 2012-09-25T19:10:05.353 回答
1
beforeShowDay: $.datepicker.noWeekends,

将会

beforeShowDay: noWeekendsOrHolidays,
于 2010-04-30T00:43:20.787 回答
1

IE 的问题很可能在以下行:

altField: '#alternate',
}); 

尝试删除逗号符号,它应该可以工作。

于 2012-04-17T09:12:18.357 回答
0
 var unavailableDates = ["19-8-2014","14-9-2014"];

 function unavailable(date) {
    dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" +date.getFullYear();
    if ($.inArray(dmy, unavailableDates) < 0) {
     return [true,"","Book Now"];
    } else {
  return [false,"","Booked Out"];
  }
 }

 $('#iDate').datepicker({ beforeShowDay: unavailable });

现场演示:http: //jsfiddle.net/vfod0krm/

于 2014-08-14T22:35:57.330 回答