javascript - jQuery Datepicker 中的禁用日期取决于模型 MVC 中的其他属性

标签 javascript jquery asp.net-mvc razor jquery-ui-datepicker

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

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

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

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

关于javascript - jQuery Datepicker 中的禁用日期取决于模型 MVC 中的其他属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37983363/

相关文章:

javascript - 连接查询参数并将它们添加到 url

javascript - Ajax 请求在 IE 9.0 中有效,但在 Firefox 24.6 中无效

javascript - 如何使用按钮更改嵌入源?

javascript - 类别/标签选择器

javascript - jquery 触发器上调用了不正确的处理程序

javascript - jquery - 限制div中的单词数

javascript - 在 JavaScript 中将负 float 转换为正 float

asp.net - 当我从 Visual Studio 运行项目时,Windows 身份验证不起作用

javascript - NumericTextBox 使用 Html.TextBoxFor 丢失值,而不是 HTML

asp.net-mvc - ASP.NET MVC3 View 模型在回发到 Controller 时不为空但缺少数据