【问题标题】:How do I disable Mondays in Datepicker?如何在 Datepicker 中禁用星期一?
【发布时间】:2021-09-08 01:53:52
【问题描述】:

有人可以在这里帮助我了解如何禁用星期一吗?
EXTRA:您如何防止有人手动输入他们自己的交货日期(即如果交货日历没有立即弹出)?

非常感谢您!

请在下面找到我当前的代码:-

if (window.jQuery) {
  let $ = window.jQuery;

  $(function() {
    // Dates to exclude
    var excludeDays = ['2021-09-04', '2021-09-12'];

    function disableSpecificDate(date) {
      // To disable specific day
      var dateArr = [String(date.getFullYear()), String(date.getMonth() + 1), String(date.getDate())];
      if (dateArr[1].length == 1) dateArr[1] = "0" + dateArr[1];
      if (dateArr[2].length == 1) dateArr[2] = "0" + dateArr[2];
      return excludeDays.indexOf(dateArr.join("-")) == -1;
    }
    $("#date").datepicker({
      dateFormat: 'dd/mm/yy',
      minDate: +1,
      maxDate: '+1M',
      beforeShow: function() {
        // To exclude next business day after 12 PM
        if (new Date().getHours() >= 12) {
          $(this).datepicker("option", "minDate", +2);
        }
      },
      beforeShowDay: function(date) {
        var day = date.getDay();
        return [(day == 0 ? false : disableSpecificDate(date)), ''];
      }
    });
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js" defer="defer"></script>

<div style="width:300px; clear:both;">
  <p>
    <input id="date" type="text" name="attributes[DELIVERY DATE]" value="" />
    <span style="display:block" class="instructions"></span>
  </p>
</div>

【问题讨论】:

    标签: jquery-ui datepicker


    【解决方案1】:

    在您的 beforeShowDay 函数中使用 day == 1。在这里我禁用了周日和周一。

    if (window.jQuery) {
      let $ = window.jQuery;
    
      $(function() {
        // Dates to exclude
        var excludeDays = ['2021-09-04', '2021-09-12'];
    
        function disableSpecificDate(date) {
          // To disable specific day
          var dateArr = [String(date.getFullYear()), String(date.getMonth() + 1), String(date.getDate())];
          if (dateArr[1].length == 1) dateArr[1] = "0" + dateArr[1];
          if (dateArr[2].length == 1) dateArr[2] = "0" + dateArr[2];
          return excludeDays.indexOf(dateArr.join("-")) == -1;
        }
        $("#date").datepicker({
          dateFormat: 'dd/mm/yy',
          minDate: +1,
          maxDate: '+1M',
          beforeShow: function() {
            // To exclude next business day after 12 PM
            if (new Date().getHours() >= 12) {
              $(this).datepicker("option", "minDate", +2);
            }
          },
          beforeShowDay: function(date) {
            var day = date.getDay();
            return [((day == 0 || day == 1) ? false : disableSpecificDate(date)), ''];
          }
        });
      });
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js" defer="defer"></script>
    
    <div style="width:300px; clear:both;">
      <p>
        <input id="date" type="text" name="attributes[DELIVERY DATE]" value="" />
        <span style="display:block" class="instructions"></span>
      </p>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 2020-10-08
      • 2021-03-12
      • 1970-01-01
      相关资源
      最近更新 更多