jQuery 日期选择器中的禁用日期取决于模型 MVC 中的其他属性

Disable days in jQuery Datepicker depend on other properties in model MVC

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

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

我环顾四周,但 none 这些选项对我有用。 这是我的代码:

$('#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');

在日历中经过大约 5-6 天后,我在控制台中收到以下错误

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

话虽如此,我已经仔细研究了此处提出的解决方案,但它可能行不通。此解决方案基于以下提议:

Disable specific days of the week on jQuery UI datepicker

提前致谢。

我检查了你的代码 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

实际上 beforeShowday 应该 return 一个数组。这就是它的文档所说的

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

*默认情况下您可以发送 null ,否则 return 和 array[flag,"",""] where flag is false for disabling