0

我正在尝试从 Jquery UI datepicker 日历中禁用某些日期。

我的问题是只有 disableDays 函数检查的最后一个日期被禁用,而不是全部。为什么它只禁用最后检查的日期。我应该从这个函数返回不同的响应吗?

完整脚本:

var disabled_days = new Array(); // Array of Date() objects


$('.date-picker-day').live('click', function () {
    $(this).datepicker({
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'dd',
        beforeShow: function () {
            [..]
        },
        beforeShowDay: disableDays,
        onClose: function (dateText, inst) {

        }

    }).focus();
});

function disableDays(date) {
    var ret = false;
    $.each(disabled_days, function (k, v) {
        if (v.getDate() == date.getDate()) {
            console.log(v + 'vs.' + date + ' invalid');
            ret = [false];
        } else {
            ret = [true];
        }
    });

    return ret;
}  
4

1 回答 1

0

You need to stop further iterations of the disabled_days array as soon as a negative match is found, else in the next value iteration the date will not match and ret will again get true value

function disableDays(date) {
    var ret = false;
    $.each(disabled_days, function (k, v) {
        if (v.getDate() == date.getDate()) {
            console.log(v + 'vs.' + date + ' invalid');
            ret = [false];
            return false;
        } else {
            ret = [true];
        }
    });
    return ret;
}

Demo: Fiddle

于 2013-08-29T12:07:36.740 回答