【问题标题】:Jquery- How to set the date before today can't be select?Jquery-如何设置今天之前的日期无法选择?
【发布时间】:2019-10-24 06:50:36
【问题描述】:

我可以让今天的日期出现在入住日期,但问题是用户仍然可以选择今天之前的日期。有什么解决方案可以让我解决他的问题吗?任何建议都会有很大帮助,谢谢!

$(function () {
        $("#chkI").datepicker({
            dateFormat: "yy-mm-dd", showAnim: "slideDown", 
            onClose: function (selectedDate) {
                var minDate = $(this).datepicker('getDate');
                var newMin = new Date(minDate.setDate(minDate.getDate() + 1));
                $("#chkO").datepicker("option", "minDate", newMin);
            }
        });
        var currentDate = new Date();
        $("#chkI").datepicker("setDate", currentDate);
        $("#chkO").datepicker({ dateFormat: "yy-mm-dd", showAnim: "slideDown",
            onClose: function (selectedDate) {
                var maxDate = $(this).datepicker('getDate');
                var newMax = new Date(maxDate.setDate(maxDate.getDate() - 1));
                $("#chkI").datepicker("option", "maxDate", newMax);
            }
        });
    });

【问题讨论】:

    标签: javascript jquery date jquery-ui-datepicker


    【解决方案1】:

    这就是我如何禁用今天之前的日期:

    $("#chkI").datepicker({
        minDate: new Date()
    });
    

    例如jsfiddle

    【讨论】:

      【解决方案2】:

      您只需要设置 minDate 并且不要为相同的输入再次初始化 datepicker

      $(function () {
                  $("#chkI").datepicker({
                      minDate: new Date(), // start date should be today's date
                      dateFormat: "yy-mm-dd", showAnim: "slideDown", 
                      onClose: function (selectedDate) {
                          var minDate = $(this).datepicker('getDate');
                          var newMin = new Date(minDate.setDate(minDate.getDate() + 1));
                          $("#chkO").datepicker("option", "minDate", newMin);
                      }
                  }).datepicker("setDate", new Date()); // select today's date by default
                  //var currentDate = new Date();
                  //$("#chkI").datepicker("setDate", currentDate);
                  $("#chkO").datepicker({ dateFormat: "yy-mm-dd", showAnim: "slideDown",
                      onClose: function (selectedDate) {
                          var maxDate = $(this).datepicker('getDate');
                          var newMax = new Date(maxDate.setDate(maxDate.getDate() - 1));
                          $("#chkI").datepicker("option", "maxDate", newMax);
                      }
                  });
              });
       <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
        <link rel="stylesheet" href="/resources/demos/style.css">
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      <input type="text" id="chkI">
      <input type="text" id="chk0">

      【讨论】:

      • 我仍然可以选择今天之前的日期在那里办理登机手续
      • @J.Boy 有一个属性minDate,可以用来禁用今天之前的日期,试试我的解决方案!
      猜你喜欢
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 2014-09-24
      • 2012-04-14
      • 2023-04-05
      相关资源
      最近更新 更多