在jQuery Datepicker中禁用天数取决于模型MVC中的其他属性

时间:2022-09-23 14:11:40

I want to disable specific days of the week in jQuery UI DatePicker , depending on whether a value is true or false in the model submitted in @razor view .

我想在jQuery UI DatePicker中禁用一周中的特定日期,具体取决于@razor视图中提交的模型中的值是true还是false。

I have a bool for each weekday . If a value is constructed so will the day be available in datepickern but if false, then the day will be disabled.

我每个工作日都有一个bool。如果构造了一个值,那么datepickern中的日期可用,但如果为false,则该日期将被禁用。

I looked around on different sides here but none of these options worked for me . Here is my code :

我在这里的不同侧面环顾四周,但这些选项都不适合我。这是我的代码:

$('#txtStartDate').datepicker({
    defaultDate: '+1w',
    numberOfMonths: 1,
    showAnim: 'slide',
    changeMonth: true,
    changeYear: true,
    showWeek: true,
    dateFormat: "yy-mm-dd",
    minDate: new Date(hidStartDate),
    beforeShowDay: function(date) {
        var day = date.getDay();

        if (day == 1 && $('#hidMonday').val() == "True") {
            return day;
        }

        if (day == 2 && $('#hidTuesday').val() == "True") {
            return day;
        }

        if (day == 3 && $('#hidWednesday').val() == "True") {
            return day;
        }

        if (day == 4 && $('#hidThursday').val() == "True") {
            return day;
        }

        if (day == 5 && $('#hidFriday').val() == "True") {
            return day;
        }
    },
});

$('#txtStartDate').css('clip', 'auto');

Once it has gone through about 5-6 days in the calendar , I get the following error in the console

一旦它在日历中经历了大约5-6天,我在控制台中收到以下错误

" Jquery - ui.js : 9742 uncaught TypeError : Can not read property '0' of undefined "

“Jquery - ui.js:9742未捕获TypeError:无法读取未定义的属性'0'

That said , I have looked around on the solution proposed here , but it may not work. This solution is based on the following proposal:

也就是说,我已经查看过这里提出的解决方案,但它可能无效。此解决方案基于以下提议:

Disable specific days of the week on jQuery UI datepicker

在jQuery UI datepicker上禁用一周中的特定日期

Thanks in advance.

提前致谢。

2 个解决方案

#1


1  

I checked your code in this fiddle

我在这个小提琴中检查了你的代码

if (day == 1 && $('#hidMonday').val() == "True") {
            return day;
        }

        if (day == 2 && $('#hidTuesday').val() == "True") {
            return day;
        }

The error is coming when no day object is returned(when it is not getting into any of the if conditions). You can not simply not return anything Better return false if any of the conditions is not met

如果没有返回任何日期对象(当它没有进入任何if条件时),则会出现错误。您不能简单地不返回任何内容如果不满足任何条件,则返回false

#2


1  

Acutally beforeShowday should return an array. That's what its document says

在显然之前应该返回一个数组。这就是它的文件所说的

beforeShowDay 

Type: Function( Date date )
Default: null
A function that takes a date as a parameter and must return an array with:
[0]: true/false indicating whether or not this date is selectable
[1]: a CSS class name to add to the date's cell or "" for the default presentation
[2]: an optional popup tooltip for this date

*By default you can send null , otherwise return an array[flag,"",""] where flag is false for disabling

*默认情况下,您可以发送null,否则返回一个数组[flag,“”,“”]其中flag为false以禁用

#1


1  

I checked your code in this fiddle

我在这个小提琴中检查了你的代码

if (day == 1 && $('#hidMonday').val() == "True") {
            return day;
        }

        if (day == 2 && $('#hidTuesday').val() == "True") {
            return day;
        }

The error is coming when no day object is returned(when it is not getting into any of the if conditions). You can not simply not return anything Better return false if any of the conditions is not met

如果没有返回任何日期对象(当它没有进入任何if条件时),则会出现错误。您不能简单地不返回任何内容如果不满足任何条件,则返回false

#2


1  

Acutally beforeShowday should return an array. That's what its document says

在显然之前应该返回一个数组。这就是它的文件所说的

beforeShowDay 

Type: Function( Date date )
Default: null
A function that takes a date as a parameter and must return an array with:
[0]: true/false indicating whether or not this date is selectable
[1]: a CSS class name to add to the date's cell or "" for the default presentation
[2]: an optional popup tooltip for this date

*By default you can send null , otherwise return an array[flag,"",""] where flag is false for disabling

*默认情况下,您可以发送null,否则返回一个数组[flag,“”,“”]其中flag为false以禁用