如何限制jquery日期范围?

时间:2017-06-22 11:00:36

标签: jquery html datepicker

我们可以将日期范围限制为15天吗?搜索日期的日期范围不能超过15天,这就是我想要阻止它的原因。我怎么能这样做?



$(function() {

  var dateFormat = "DD.MM.YY",
    from = $("#checkin,.checkin").datepicker({
      numberOfMonths: 2,
      firstDay: 1,
      minDate: 0,
    }),
    to = $("#checkout,.checkout").datepicker({
      firstDay: 1,
      numberOfMonths: 2,
      minDate: 0,
    });


});

.selected-range {
  background: red;
  color: #FFF;
}

<link href="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.0/css/smoothness/jquery-ui-1.10.0.custom.min.css" rel="stylesheet" />

<div class="book-holiday">
  <input type="text" id="checkin" />
  <input type="text" id="checkout" />
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

在选项

中添加maxDate: 15

$(function() {

  var dateFormat = "DD.MM.YY",
    from = $("#checkin,.checkin").datepicker({
      numberOfMonths: 2,
      firstDay: 1,
      minDate: 0,
      maxDate: 15
    }),
    to = $("#checkout,.checkout").datepicker({
      firstDay: 1,
      numberOfMonths: 2,
      minDate: 0,
      maxDate: 15
    });


});
.selected-range {
  background: red;
  color: #FFF;
}
<link href="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.0/css/smoothness/jquery-ui-1.10.0.custom.min.css" rel="stylesheet" />

<div class="book-holiday">
  <input type="text" id="checkin" />
  <input type="text" id="checkout" />
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>