首页 > 解决方案 > 开始日期不应大于结束日期 jquery php

问题描述

我有两个日期选择器,我希望开始日期不应大于结束日期或结束日期不应小于使用 jquery 的开始日期,我尝试使用以下代码但不适合我,我该怎么做?

<script>
$(function() {  
      $( "#to" ).datepicker({   
      defaultDate: "",
         maxDate:"+0d",
      changeMonth: false,   
      numberOfMonths: 1,  
      onClose: function( selectedDate ) {  
        $( "#from" ).datepicker( "option", "minDate" ,selectedDate ); 
      }  
    });  

  });  
 </script>

<script>
$(function() {  
      $( "#from" ).datepicker({  

      defaultDate: "",
         maxDate:"+0d",
      changeMonth: false,   
      numberOfMonths: 1,  
      onClose: function( selectedDate ) {  
        $( "#to" ).datepicker( "option", "maxDate" ,selectedDate ); 
       }  
    });  

  });  
 </script>

标签: javascriptjquery

解决方案


您可以使用 onSelect 事件

$(function() {
  $("#from").datepicker({
    defaultDate: "",
    maxDate: "+0d",
    changeMonth: false,
    numberOfMonths: 1,

    onSelect: function(selected) {
      $("#to").datepicker("option", "minDate", selected)
    }
  });

  $("#to").datepicker({
    defaultDate: "",
    maxDate: "+0d",
    changeMonth: false,
    numberOfMonths: 1,

    onSelect: function(selected) {
      $("#from").datepicker("option", "maxDate", selected)
    }
  });
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<input type="text" id="from" />
<input type="text" id="to" />


推荐阅读