datepicker没有正确填充日期

时间:2012-04-02 14:44:30

标签: javascript jquery-ui

这是我的jquery代码:

   var unavailableDates = ["28-06-2011","22-12-2011","22-10-2011","22-02-2012",
                           "21-10-2011","21-07-2011","19-10-2011","18-11-2011",
                           "18-10-2011","18-05-2011","18-04-2011","17-12-2010",
                           "17-10-2011","14-10-2011","13-02-2012","12-11-2011",
                           "12-04-2011","09-12-2011","09-05-2011","09-02-2012",
                           "07-11-2011","07-03-2012","05-07-2011","03-05-2011",
                           "02-12-2011","01-11-2011","01-08-2011","01-06-2011"];


function unavailable(date) {
    dmy = date.getDate() + "-"+ (date.getMonth() + 1) +"-" + date.getFullYear();
    if ($.inArray(dmy, unavailableDates) == -1) {
        return [false, "Unavailable"];
    } else {
        return [true, "", ""];
    }
}

$(function() {
$("#iDate").datepicker({
    dateFormat: 'yy-mm-dd',
    beforeShowDay: unavailable,
    onSelect: function (dateText, inst) {
        $('#frmDate').submit();
     }
  });
});

由于某种原因,它使用数组中的某些日期填充了一些datepicker控制器,但省略了其他日期。我不知道它为什么会这样表现。

有人可以帮忙吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

unavailable()包含错误的逻辑。当getDate()和getMonth()返回单个数字时,您尝试查找数组中不存在的日期。例如,您搜索“1-8-2011”而不是“01-08-2011”。 一种解决方案是在必要时用零填充你的日期部分。

JsFiddle example