gpt4 book ai didi

javascript - 如果没有可用日期,jquery html日历不会从数组中选择日期

转载 作者:行者123 更新时间:2023-11-27 22:41:59 24 4
gpt4 key购买 nike

仍在尝试掌握 JQuery。不太擅长。

我的 html 日历有问题。我可以在自动选择当前月份的情况下加载它。更改下拉选择时,它会切换到该月份,但不会选择“不可用”的日期进行预订。

这是代码:

        var cal = new Calendar();

var unavailable_days_month_1 = [1,2,3];
var unavailable_days_month_2 = [4,5,6];
var unavailable_days_month_3 = [7,8,9];
var unavailable_days_month_4 = [10,11,12];
var unavailable_days_month_5 = [13,14,15];
var unavailable_days_month_6 = [16,17,18];
var unavailable_days_month_7 = [19,20,21];
var unavailable_days_month_8 = [22,23,24];
var unavailable_days_month_9 = [25,26,27];
var unavailable_days_month_10 = [28,29,30];
var unavailable_days_month_11 = [2,4,31];
var unavailable_days_month_12 = [7,9,11];

var current_date = new Date();
var current_month = (current_date.getMonth() + 1);
var current_year_month = (1900 + current_date.getYear()) + "-" + current_month;
tjq("#select-month").find("[value='" + current_year_month + "']").prop("selected", "selected");
cal.generateHTML(current_date.getMonth(), (1900 + current_date.getYear()), "unavailable_days_month_" + current_month);
tjq(".calendar").html(cal.getHTML());

tjq("#select-month").change(function() {
var selected_year_month = tjq("#select-month option:selected").val();
var year = parseInt(selected_year_month.split("-")[0], 10);
var month = parseInt(selected_year_month.split("-")[1], 10);
/* My problem starts from here */
cal.generateHTML(month - 1, year, "unavailable_days_month_" + month);
tjq(".calendar").html(cal.getHTML());
});

提前致谢。

最佳答案

您的问题是您返回的是字符串,而不是数组。引用:

"unavailable_days_month_" + month

所以,如果我是你,如果你想做一些在大多数情况下应该有效的事情,如果用户不利用它,我会创建一个函数。

function getUnavailDays (month) {
if (month === 1) return unavailable_days_month_1
if (month === 2) return unavailable_days_month_2
if (month === 3) return unavailable_days_month_3
// and so on...
return // return if none of the cases match above.
}

这可能不是最好的解决方案,但至少是某种

稍后您可以使用 getUnavailDays(month) 调用它。

编辑:

更好的解决方案;

function getUnavailDays (month) {
switch (month) {
case 1:
return unavailable_days_month_1
case 2:
return unavailable_days_month_2
// and so on..
default:
return
}
}

关于javascript - 如果没有可用日期,jquery html日历不会从数组中选择日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38687105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com