javascript Jquery datepicker 启用数组中的日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33643253/
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-10-28 16:44:21  来源:igfitidea点击:

Jquery datepicker enable dates from array?

javascriptjquerydatepicker

提问by Tony

I am trying to enable just some specified dates in datepicker. In my case the values inside the variable "onlyThisDates". Should I use enabledDates option to solve this or ..? I do not want disable the values in the array. The other way around. I want disable everything and just enable the values in the array.

我试图在 datepicker 中只启用一些指定的日期。在我的情况下,变量“onlyThisDates”中的值。我应该使用 enabledDates 选项来解决这个问题还是..?我不想禁用数组中的值。另一种方式。我想禁用所有内容,只启用数组中的值。

If someone could help it would be great.

如果有人可以提供帮助,那就太好了。

<input id="openDatepick></input>
var onlyThisDates = ['09/11/2015', '10/11/2015', '11/11/2015'];    

var nowDate = new Date();
var today = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 0, 0, 0, 0);

$('#openDatepick').datepicker({
    format: 'dd/mm/yyyy',
    startDate: today

}).on('changeDate', function (e) {
    //$('this').datepicker('hide');
});
$("#remove").on("click", function () {
    $('.datepicker-days').hide();
});

回答by Taleeb

In beforeShowDayfunction: for any date you return false - that will be disabled and for any date any thing other than false is returned that day will be enabled.

beforeShowDay功能上:对于您返回 false 的任何日期 - 这将被禁用,对于任何日期,该日将启用除 f​​alse 以外的任何其他内容。

Based on this your beforeShowDayshould be something like the following:-

基于此,您beforeShowDay应该类似于以下内容:-

beforeShowDay: function (date) {
        var dt_ddmmyyyy = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();
        return (onlyThisDates.indexOf(dt_ddmmyyyy) != -1);
    }

Note that I've added 1 to getMonth()as month in javascript starts from 0.

请注意,我getMonth()在 javascript 中从 0 开始添加了 1 到as 月份。

See jsFiddle here.

请参阅此处的 jsFiddle



EDIT: Based on comment

编辑:基于评论

In the beforeShowDayfunction we can define css, tooltips etc. For example instead of returning true we can return a css-class.

beforeShowDay函数中,我们可以定义 css、工具提示等。例如,我们可以返回一个 css-class,而不是返回 true。

beforeShowDay: function (date) {
        var dt_ddmmyyyy = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();
        if (onlyThisDates.indexOf(dt_ddmmyyyy) != -1) {
            return {
                tooltip: 'This date is enabled',
                classes: 'active'
            };
        } else {
            return false;
        }
    }

See this jsFiddle.

看到这个jsFiddle

Also see this linkfor different functionalities available with bootstrap datepicker.

另请参阅此链接以了解引导程序日期选择器可用的不同功能。