Py学习  »  Jquery

如何向Jquery UI Datepicker日期范围再添加一天?

tonydeleon • 3 年前 • 1022 次点击  

如果我输入日期,我的代码可以正常工作,但是如果我点击“.from_date”并将其留空,我会出现以下错误:“无法读取null的属性'setDate'。我能做些什么来避免这个错误?

    $(".from_date").datepicker({
      minDate: 0,
      dateFormat: "dd/mm/y",
      defaultDate: "+1w",
      numberOfMonths: 1,
      onClose: function() {
      var minDate = $(this).datepicker('getDate');
      var newMin = new Date(minDate.setDate(minDate.getDate() + 1));
        $(".to_date").datepicker("option", "minDate", newMin);
        return $(".to_date").datepicker("show");
      }
    });
 $(".to_date").datepicker({
      minDate: '+1D',
      dateFormat: "dd/mm/y",
      defaultDate: "+1w",
      numberOfMonths: 1
    });
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/127986
 
1022 次点击  
文章 [ 1 ]  |  最新文章 3 年前
Twisty
Reply   •   1 楼
Twisty    4 年前

考虑下面的例子。

$(function() {
  $(".from_date").datepicker({
    minDate: 0,
    dateFormat: "dd/mm/y",
    defaultDate: "+1w",
    numberOfMonths: 1,
    onClose: function(dText) {
      var minDate = $.datepicker.parseDate("dd/mm/y", dText);
      if (minDate != null) {
        var newMin = new Date(minDate.setDate(minDate.getDate() + 8));
        $(".to_date").datepicker("option", "minDate", newMin);
      }
    }
  }).datepicker("setDate", "+1w");
  $(".to_date").datepicker({
    minDate: "+8d",
    dateFormat: "dd/mm/y",
    defaultDate: "+8d",
    numberOfMonths: 1
  }).datepicker("setDate", "+8d");;
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.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>
<label for="from">From</label>
<input type="text" id="from" class="from_date" name="from">
<label for="to">to</label>
<input type="text" id="to" class="to_date" name="to">

在这里,我们用 setDate 方法这可以确保两个字段中都有日期,因此如果用户选择或不选择日期,他们可以无误地继续操作。