2017-01-10 117 views
1

我有两个只选择月份和年份的日期选择器。我需要验证这两个日期选择器。如果我从onedatepicker(starDate)中选择一个月和一年,则应在seconddatepicker(endDate)中禁用2017年2月和之前的所有日期(如2017年1月,2016年12月,2016年11月等)。我设置了一个minValue但不起作用。如何在jquery datepicker中无日期验证开始和结束日期。

这是我fiddle

<p>Start Date 1: <input type="text" id="startDate"></p> 
<p>End Date 1: <input type="text" id="endDate"></p> 

<script> 
    $("#startDate").datepicker({ 
     changeMonth: true, 
     changeYear: true, 
     beforeShowDay: function (date) { 
      return ['']; 
     }, 
     showButtonPanel: true, 
     dateFormat: 'MM yy', 
     onClose: function (dateText, inst) { 
       var minValue = $(this).val(); 
      jQuery(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1)); 
      $("#endDate").datepicker({ 
       changeMonth: true, 
       changeYear: true, 
       beforeShowDay: function (date) { 
        return ['']; 
       }, 
       showButtonPanel: true, 
       dateFormat: 'MM yy', 
       minValue:minValue, 
       onClose: function (dateText, inst) { 
        jQuery(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1)); 
       }, 
      }); 
     }, 
    }); 

<script> 

感谢。 :)

+0

重复:http://stackoverflow.com/questions/3827425/jquery-ui-datepicker-range – deepakssn

回答

1

得到了我的答案。 :)

小提琴:https://jsfiddle.net/n1gzbkru/2/

$("#startDate").datepicker({ 
     changeMonth: true, 
     changeYear: true, 
     beforeShowDay: function (date) { 
      return ['']; 
     }, 
     showButtonPanel: true, 
     dateFormat: 'MM yy', 
     onClose: function (dateText,inst) { 
      $(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1)); 
      var month = inst.selectedMonth; 
      console.log(month); 
      $("#endDate").datepicker({ 
       changeMonth: true, 
       changeYear: true, 
       minDate:"+"+(month+1)+"m", 
       beforeShowDay: function (date) { 
        return ['']; 
       }, 
       showButtonPanel: true, 
       dateFormat: 'MM yy', 
       onClose: function (dateText,inst) { 
        jQuery(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1)); 
       }, 
      }); 
     }, 
    }); 
相关问题