首页 文章

jQuery Datepicker如何在beforeShowDay中添加日期数组

提问于
浏览
0

我有一个jQuery Datepicker日历用于旅游预订 . 一周中的某些日子不适用于预订,但这些截止日期取决于月份(季节) . 因此,例如12月除了星期日之外每天都有,11月根本不可用,4月每周三,周五和周日都不可用 . 到目前为止这是有效的 .

我现在想要添加一个固定日期的数组,这些日期应该被阻止(例如:2013年12月7日,2013年12月11日),但我很难让它运作起来 .

这是我目前的代码:

$("#tripDate").datepicker({
    numberOfMonths: 2,
    showButtonPanel: true,
    dateFormat: 'dd.mm.yy',
    firstDay: 1,
    minDate: +2,
    showOn: "both",
    buttonImage: "images/cal.png",
    buttonImageOnly: true,
    beforeShowDay: function unavailable(date) {
        var year = date.getYear();
        var day = date.getDay();
        var month = date.getMonth();
        if (month == 10) {
            return [(day == 8)];
        } else if (month == 3 || month == 4 || month == 5 || month == 8 || month == 9) {
            return [(day != 3 && day != 5 && day != 0)];
        } else {
            return [(day != 0)];

        }
    }
});

1 回答

  • 1

    尝试

    var array = ['07.12.2013', '11.12.2013'];
    $("#tripDate").datepicker({
        numberOfMonths: 2,
        showButtonPanel: true,
        dateFormat: 'dd.mm.yy',
        firstDay: 1,
        minDate: +2,
        showOn: "both",
        buttonImage: "images/cal.png",
        buttonImageOnly: true,
        beforeShowDay: function unavailable(date) {
            var f = $.datepicker.formatDate('dd.mm.yy', date)
            var year = date.getYear();
            var day = date.getDay();
            var month = date.getMonth();
            if ($.inArray(f, array) > -1) {
                return [false];
            } else if (month == 10) {
                return [(day == 8)];
            } else if (month == 3 || month == 4 || month == 5 || month == 8 || month == 9) {
                return [(day != 3 && day != 5 && day != 0)];
            } else {
                return [(day != 0)];
    
            }
        }
    });
    

    演示:Fiddle

相关问题