为什么datepicker beforeShowDay没有按预期运行

时间:2012-01-30 13:54:29

标签: javascript jquery jquery-ui-datepicker

我遇到了一个我创建的日期选择器的问题。我在同一页面上使用了一个非常相似的日期选择器,完全没有问题。日期选择器显示没有可选日期,即使我调试'limitToOneYear'函数时,它会在某些天返回true。我错过了一些简单而明显的东西吗?

    function limitToOneYear(date) {
        var thisdate = Date.parse(date);
        var now = new Date();
        var today = Date.parse(new Date(now.getFullYear(),now.getMonth(),now.getDate()));
        var maxDate = Date.parse(new Date((now.getFullYear() + 1), now.getMonth(), now.getDate()));
        return (thisdate >= today && thisdate < maxDate);
    }

    $(".expirationDate").datepicker({
        showOn: 'both',
        buttonImage: '/images/datepicker/button.gif',
        buttonImageOnly: true,
        beforeShowDay: limitToOneYear
    });

2 个答案:

答案 0 :(得分:1)

我找到了一个解决方案,所以我发布了它,我仍然想知道为什么我以前的解决方案不起作用所以我会选择一个不同的答案,如果它出现,但我最终利用了minDate maxDate datepicker的属性:

    var now = new Date();
    var endDate = now;
    endDate = new Date((now.getFullYear() + 1), now.getMonth(), now.getDate());
    $(".expirationDate").datepicker({
        showOn: 'both',
        buttonImage: '/images/datepicker/button.gif',
        buttonImageOnly: true,
        minDate : now, 
        maxDate: endDate
    });

答案 1 :(得分:1)

你是否应该根据beforeShowDay的DatePicker API从limitToOneYear函数返回一个数组

所以你的limitToOneYear函数应该是这样的

function limitToOneYear(date) {         
    var thisdate = Date.parse(date);         
    var now = new Date();         
    var today = Date.parse(new Date(now.getFullYear(),now.getMonth(),now.getDate()));         
    var maxDate = Date.parse(new Date((now.getFullYear() + 1), now.getMonth(), now.getDate()));         
    return [(thisdate >= today && thisdate < maxDate), '']; 
} 
相关问题