Jquery BeforeShowDay选择偶数或奇数

时间:2014-11-19 00:38:11

标签: jquery datepicker jquery-ui-datepicker

我正在尝试一些东西,但我无法弄清楚问题。我有日历。如果我选择连月份,只有偶数日期应该是可选择的,如果我选择奇数月份,只有奇数日期应该是可选择的。但我的问题,当我选择奇数月它显示所有日期,当我选择甚至月份它没有显示任何日期。 http://jsfiddle.net/66688hft/1/

    <!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Datepicker - Default functionality</title>
  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/ui-lightness/jquery-ui.css" type="text/css" rel="stylesheet" />
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
    $( "#datepicker" ).datepicker({
      changeMonth: true,
      changeYear: true,
      showOn: "button",
      buttonImage: "calendar.gif",
      buttonImageOnly: true,
      buttonText: "Select date",
           beforeShowDay: function(date) {
        var day = $(this).datepicker('getDate');
        var month = date.getMonth()+1;
    if(month%2==1)
    return [day % 2 == 1];

  else
    return [day % 2 == 0];

}

    });
  });
  </script>
</head>
<body>

<p>Date: <input type="text" id="datepicker"></p>


</body>
</html>

1 个答案:

答案 0 :(得分:0)

大卫是对的,你没有正确地度过这一天。试试这个:

$(function() {
  $( "#datepicker" ).datepicker({
      changeMonth: true,
      changeYear: true,
      showOn: "button",
      buttonImage: "calendar.gif",
      buttonImageOnly: true,
      buttonText: "Select date",
      beforeShowDay: function(date) {
          var day = date.getDate(),
              month = date.getMonth()+1;
          return [day % 2 == month % 2, ''];
      }
  });
}