2010-04-23 128 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

相關問題