禁用除jQuery UI Datepicker中的一个星期日以外的所有星期日

时间:2012-02-24 14:15:58

标签: jquery jquery-ui datepicker jquery-ui-datepicker

我正在尝试为我的客户的网站设置jQuery UI datepicker,它需要隐藏某些日子,以及所有星期日除了母亲周日之外。目前我的代码,从另一个答案中大量借用的代码如下:

        function nonWorkingDates(date){
            var day = date.getDay(), Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6;
            var closedDates = [[02,24,2012],[03,14,2012],[03,15,2012],[03,20,2012],[03,21,2012],[03,23,2012],[03,26,2012],[03,27,2012]];
            var closedDays = [[Sunday]];  
            var mothersDay = [[04,18,2012]];  
            for (var i = 0; i < closedDays.length; i++) {
                if (day == closedDays[i][0]) {
                    return [false];
                }
            }
            for (i = 0; i < closedDates.length; i++) {
                if (date.getMonth() == closedDates[i][0] - 1 && date.getDate() == closedDates[i][1] && date.getFullYear() == closedDates[i][2]) {
                    return [false];
                }
            }
            return [true];
        }       
        <%-- Load jQuery UI Calendar --%>
        $(function() {
            jQuery("#calendar1").datepicker({
            beforeShowDay: nonWorkingDates,
            dateFormat: 'dd/mm/yy'
            });         
        });

有没有简单的方法可以做到这一点?

1 个答案:

答案 0 :(得分:1)

您可以在其他逻辑之前为“特殊”日期添加另一个条件:

function nonWorkingDates(date) {
    var day = date.getDay(),
        Sunday = 0,
        Monday = 1,
        Tuesday = 2,
        Wednesday = 3,
        Thursday = 4,
        Friday = 5,
        Saturday = 6;
    var closedDates = [
        [02, 24, 2012],
        [03, 14, 2012],
        [03, 15, 2012],
        [03, 20, 2012],
        [03, 21, 2012],
        [03, 23, 2012],
        [03, 26, 2012],
        [03, 27, 2012]];

    var closedDays = [[Sunday]];
    var mothersDay = [03, 18, 2012];  

    /* Mother's day check: */
    if (date.getMonth() === mothersDay[0] - 1 &&
        date.getDate() === mothersDay[1] &&
        date.getFullYear() === mothersDay[2]) {

            return [true];
    }                                 

    for (var i = 0; i < closedDays.length; i++) {
        if (day == closedDays[i][0]) {
            return [false];
        }
    }
    for (i = 0; i < closedDates.length; i++) {
        if (date.getMonth() == closedDates[i][0] - 1 &&
            date.getDate() == closedDates[i][1] &&
            date.getFullYear() == closedDates[i][2]) {

            return [false];
        }
    }
    return [true];
}

示例: http://jsfiddle.net/6wwSn/