2010-04-23 61 views
1

因此,如果我的开始日期是:04/22/2010,那么我的结束日期选择可以高达04/22/2011,并且在04/22/2011之后的日期都是禁用的。JQuery datePicker:开始/结束日期应该在一年之内

这里是我有两个选择开始和结束日期:

$(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(); } 
    }); 
}); 

回答

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

+0

谢谢尼克.... – 2010-04-23 03:00:25

相关问题