在月中阻止偶数天,天数为偶数 (JQuery UI)

Blocking even days in Months with even number of days (JQuery UI)

本文关键字:JQuery UI      更新时间:2023-09-26

我必须制作日期选择器,它将像这样工作:如果所选月份有偶数天(30),则该月中的所有偶数天将被阻止选择(2,4,6...)等。如果所选月份的天数为奇数(31),则日期选择器中的所有奇数天将被阻止选择(1,3,5..)等。

我的问题是我不知道如何阻止偶数和奇数天,我只知道如何阻止整个月,而不是任何一天......免责声明:我知道已经发布了一些内容,例如如何阻止特定日期,但没有任何偶数或奇数天的内容。

这是我的代码:

  <link rel="stylesheet" href="jquery-ui.min.css">
  <script src="external/jquery/jquery.js"></script>
  <script src="jquery-ui.min.js"></script>
  <script>
  $(function() {
   $( "#datepicker" ).datepicker({beforeShowDay: nationalDays});
  });
 function nationalDays(date){
 var x = date.getMonth();
 var r = x%2;
 if(r == 0){
    return [false];
 }
else if(r == 1){
    return [true];
 }
}
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>

</body>

在 beforeShowDay 选项中,您可以检查是否应该启用日期,并将数组中返回的第一项设置为 true/false 。True 将启用该项目,而 false 将禁用它,如下所示:

$(function() {
  $("#datepicker").datepicker({
    defaultDate: new Date(),
    beforeShowDay: function(date) {
      var disabled = true, // date enabled by default
        // get the number of days in current month
        numOfDays = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
      if (numOfDays % 2 == 0)
        disabled = (date.getDate() % 2 != 0) // for even-days months, disable the even dates
      else disabled = (date.getDate() % 2 == 0) //for odd - days months, disable the odd dates
      return [disabled, ""]
    }
  });
});
<link href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<p>Date:
  <input type="text" id="datepicker" />
</p>