jQuery UI Datepicker - 禁用特定日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/677976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:33:58  来源:igfitidea点击:

jQuery UI Datepicker - Disable specific days

jqueryjquery-uidatepicker

提问by Sam

Is there any (easy) way to set the jQuery UI Datepicker to disallow selection of specific, predetermined days?

是否有任何(简单)方法可以设置 jQuery UI Datepicker 以禁止选择特定的预定日期?

I was able to get this approachworking, however, it produces a null error which prevents it from displaying in IE.

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

'natDays[...].0' is null or not an object

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

Thanks in advance!

提前致谢!

UPDATE:Might help if I included some code, right? Anyway, most of this was taken straight from the aforementioned thread:

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

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',
        }); 
    });

Thanks again!

再次感谢!

采纳答案by Chris Haines

Have you tried declaring natDays properly with the 'var' keyword in front?

您是否尝试使用前面的“var”关键字正确声明 natDays?

Also - you have an extra comma at the end of your natDays definition.

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

natDays[i][2] won't work as your the arrays only have 2 items in them - try just ''

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

Additionally, you'll want to set your beforeShowDay function name correctly - doesn't look like it's even calling your custom function

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

回答by Tokes

Here is a way to disable specific dates from being selected:

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

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 });

Source: jQuery - Datepicker - Disable Specific Dates

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

回答by Alex K

You can use the beforeShowDayoption. I needed to disable any day past the 28th of the month. Here is my code.

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

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

Here is more information about it: http://api.jqueryui.com/datepicker/#option-beforeShowDay

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

The date variable passed into the beforeShowDay callback is a JavaScript date object, so it can be formatted using various libraries, a timestamp can be retrieved using date.getTime(), etc.

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

回答by allen

beforeShowDay: $.datepicker.noWeekends,

will be

将会

beforeShowDay: noWeekendsOrHolidays,

回答by Lukas Celovsky

The problem with IE is most probably on the following line:

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

altField: '#alternate',
}); 

Try to remove the comma symbol and it should work.

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

回答by Roman

 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 });

Live Demo: http://jsfiddle.net/vfod0krm/

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