【问题标题】:JQuery datePicker: start/end date should be within one yearJQuery datePicker:开始/结束日期应在一年内
【发布时间】:2010-04-23 02:08:05
【问题描述】:

因此,如果我的开始日期是:04/22/2010,那么我的结束日期选择最多可以是 04/22/2011,而在 04/22/2011 之后的日期全部禁用。

以下是我的选择开始日期和结束日期:

$(document).ready(function () {      
  $('#endDate').datepicker({ showOn: 'button', 
      buttonImage: '../images/Calendar.png', 
      buttonImageOnly: true, onSelect: function () { }, 
      onClose: function () { $(this).focus(); } 
    }); 

  $('#startDate').datepicker({ showOn: 'button', 
      buttonImage: '../images/Calendar.png', 
      buttonImageOnly: true, onSelect: 
        function (dateText, inst) { 
          $('#endDate').datepicker("option", 'minDate', new Date(dateText)); 
        } 
      , 
      onClose: function () { $(this).focus(); } 
    }); 
}); 

【问题讨论】:

    标签: jquery jquery-ui datepicker


    【解决方案1】:

    你可以同时设置maxDate选项,像这样:

    $(document).ready(function () {      
      $('#endDate').datepicker({ showOn: 'button', 
          buttonImage: '../images/Calendar.png', 
          buttonImageOnly: true, onSelect: function () { }, 
          onClose: function () { $(this).focus(); } 
      }); 
    
      $('#startDate').datepicker({ showOn: 'button', 
          buttonImage: '../images/Calendar.png', 
          buttonImageOnly: true, 
          onSelect: function (dateText, inst) { 
              var nyd = new Date(dateText);
              nyd.setFullYear(nyd.getFullYear()+1);
              $('#endDate').datepicker("option", { minDate: new Date(dateText), 
                                                   maxDate: nyd });
          }, 
          onClose: function () { $(this).focus(); } 
      }); 
    }); ​
    

    You can play with a working demo here

    这只是获取日期,添加一年(不要使用.getYear/.setYear,它们已被弃用)并使用该日期在另一个日期选择器上设置maxDate 属性。如果你好奇,there's a helpful maintained list of javascript Date methods here

    【讨论】:

      猜你喜欢
      • 2016-11-03
      • 2021-07-16
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 2018-03-06
      相关资源
      最近更新 更多