jQuery UI datepicker date range

后端 未结 2 1012
攒了一身酷
攒了一身酷 2021-01-01 06:34

I am trying to setup two date pickers like this http://jqueryui.com/demos/datepicker/#date-range. But if you manually type in a date into that example you can break the code

相关标签:
2条回答
  • 2021-01-01 06:42
    1. first idea
      • use the beforeShow instead of onSelect to set the max/min date range. You still can enter manually wrong values, but as soon as you try to open a datepicker it will auto correct itself.
      • to avoid manual tampering, you can make readonly the fields.
    2. second idea
      • use the beforeShow and at the same time do some manual checking at the change event of the fields. ( http://jsfiddle.net/s3h5L/4/ )
    0 讨论(0)
  • 2021-01-01 07:01

    this works ok for me but cannot change from date after click once . (by mistake) . i guess in from date picker all dates can be selectable instead of future dates

    <script>    
    $(function() {
        var dates = $( "#datepicker1, #datepicker2" ).datepicker({
            changeMonth: true, changeYear: true,dateFormat: "yy-mm-dd",
    
    
            beforeShow: function( ) {
                var other = this.id == "from" ? "#datepicker2" : "#datepicker1";
                var option = this.id == "from" ? "maxDate" : "minDate";
                var selectedDate = $(other).datepicker('getDate');
    
                $(this).datepicker( "option", option, selectedDate );
            }
        }).change(function(){
            var other = this.id == "from" ? "#datepicker2" : "#datepicker1";
    
            if ( $('#datepicker1').datepicker('getDate') > $('#datepicker2').datepicker('getDate') )
                $(other).datepicker('setDate', $(this).datepicker('getDate') );
        });
    });
    </script>
    
    0 讨论(0)
提交回复
热议问题