2012-12-19 100 views
3

以下片段屬於選擇兩個日期,我要補充的另一個特徵是即結束日期的選擇應限於一天GREATERTHAN開始日期jQuery中使用起始日期限制結束日期

$(function() { 
    $("#fromDate").datepicker({ 
     defaultDate: "+0", 
     changeMonth: true, 
     changeYear: true, 
     numberOfMonths: 1, 
     dateFormat:"yy-mm-dd", 
     onClose: function(selectedDate) { 
      $("#toDate").datepicker("option", "minDate", selectedDate); 
      this.focus(); 
     } 
    }); 
    $("#toDate").datepicker({ 
     defaultDate: "+0", 
     changeMonth: true, 
     changeYear: true, 
     numberOfMonths: 1, 
     dateFormat:"yy-mm-dd", 
     onClose: function(selectedDate) { 
      $("#fromDate").datepicker("option", "maxDate", selectedDate); 
     } 
    }); 
}); 

THX的提前!!!

回答

0

簡單的解決你的問題,只是兩行JS代碼和賓果!。 只需更改的數量按規定(如果你想放的最多天數爲1或更大)

$("#from").datepicker({ 
       defaultDate: "+1w", 
       changeMonth: true, 
       numberOfMonths: 1, 
       onClose: function (selectedDate) { 
        var d = new Date(selectedDate); 
        d.setDate(d.getDate() + 1); 
        $("#to").datepicker("option", "minDate", selectedDate); 
        $("#to").datepicker("option", "maxDate", d); 
       } 
      }); 
      $("#to").datepicker({ 
       defaultDate: "+1w", 
       changeMonth: true, 
       numberOfMonths: 1, 
       onClose: function (selectedDate) { 
        $("#from").datepicker("option", "maxDate", selectedDate); 
       } 
      }); 
+0

嗨prashant kankhara thx爲解決我的問題與最小代碼 – user1711663

0

我不知道這是正確的方法,但它會工作,因爲你需要

$(function() { 
     $("#from").datepicker({ 
      defaultDate: "+1w", 
      changeMonth: true, 
      numberOfMonths: 3, 
      onClose: function(selectedDate) { 
       var today = new Date(selectedDate); 
       var tomorrow = new Date(today.getTime() + (24 * 60 * 60 * 1000)); 
       var curr_date = tomorrow.getDate(); 
       var curr_month = tomorrow.getMonth() + 1; //Months are zero based 
       var curr_year = tomorrow.getFullYear(); 

       var max_string = curr_month+"/"+curr_date+"/"+curr_year; 

       $("#to").datepicker("option", "minDate", selectedDate); 
       $("#to").datepicker("option", "maxDate", max_string); 
      } 
     }); 
     $("#to").datepicker({ 
      defaultDate: "+1w", 
      changeMonth: true, 
      numberOfMonths: 3, 
      onClose: function(selectedDate) { 
       $("#from").datepicker("option", "maxDate", selectedDate); 
      } 
     }); 
    }); 
+0

THX的答覆,但它不適合我:),我的要求是 - >結束日期應大於或等於開始日期的一天,不得超過 – user1711663

+0

已更新。請檢查 –

+0

工作演示http://jsfiddle.net/bjkRA/ –

0
$(document).ready(function(){ 
    $("#txtFromDate").datepicker({ 
     numberOfMonths: 2, 
     onSelect: function(selected) { 
      $("#txtToDate").datepicker("option","minDate", selected) 
     } 
    }); 
    $("#txtToDate").datepicker({ 
     numberOfMonths: 2, 
     onSelect: function(selected) { 
      $("#txtFromDate").datepicker("option","maxDate", selected) 
     } 
    }); 
}); 
相關問題