我想在 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/