jQuery Datepicker - 仅选择几天

时间:2013-03-11 19:05:26

标签: jquery jquery-ui-datepicker

我试图让我的日期选择器更好,但我只设法启用(首先我禁用所有)星期一和星期四但现在我有特定日期的问题。我尝试了许多可能性,但要么每个月选择第21个,要么根本不工作。

现在我试图启用该日期(2013,04,21)(以及后来可能更多)

  $( '#vfb-datum-75' ).datepicker({
              beforeShowDay: settings,
  });  

  function settings(date) {
      if (date.getDay() == 1)       
          { return [true, "","Works"];
      } else if (date.getDay() == 4)
          { return [true, "","Works"];
      } else if (date.getDate() == (2013,04,21))  <-- this doesn't work
          { return [true, "","Works"];
      } else { return [false,"",""]; 
  }}

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

http://jsfiddle.net/f7kAc/

您可以比较年,日和月。请记住,月份为零。

date.getDate() == 21 && date.getMonth() == 3 && date.getFullYear() == 2013

http://jsfiddle.net/f7kAc/1/

您也可以使用此方法将日期预定到另一个日期。

date - new Date(2013, 3, 21) == 0

但是,如果你要将这个功能增加到50多个特殊日期,你可能会想要一些更快捷的东西,并且更容易配置。

var specialDays = 
{2013 : 
  {
      3 : 
          {
              21 : [true, "", "WORKS"]
          },
      4 : 
          {
              15 : [true, "", "WORKS"],
              17 : [true, "", "WORKS"]
          },
  },
};

然后在你的函数中:

... else if (!!specialDays[date.getYear()] && !!specialDays[date.getYear()][date.getMonth()] && !!specialDays[date.getYear()][date.getMonth()][date.getDate()]) {
  return specialDays[date.getYear()][date.getMonth()][date.getDate()];
}