jQuery datepicker to prevent past date

后端 未结 15 2828
生来不讨喜
生来不讨喜 2020-12-04 16:34

How do I disable past dates on jQuery datepicker? I looked for options but don\'t seem to find anything that indicates the ability to disable past dates.

UPDATE: Th

相关标签:
15条回答
  • 2020-12-04 16:53

    Try setting startDate to the current date. For example:

    $('.date-pick').datePicker({startDate:'01/01/1996'});
    
    0 讨论(0)
  • 2020-12-04 16:58

    Give zero to mindate and it'll disabale past dates.

    $( "#datepicker" ).datepicker({ minDate: 0});
    

    Live example

    read more here

    0 讨论(0)
  • 2020-12-04 17:02

    Make sure you put multiple properties in the same line (since you only showed 1 line of code, you may have had the same problem I did).

    Mine would set the default date, but didn't gray out old dates. This doesn't work:

    $('#date_end').datepicker({ defaultDate: +31 })
    $('#date_end').datepicker({ minDate: 1 })
    

    This does both:

    $('#date_end').datepicker({ defaultDate: +31, minDate: 1 })
    

    1 and +1 work the same (or 0 and +0 in your case).

    Me: Windows 7 x64, Rails 3.2.3, Ruby 1.9.3

    0 讨论(0)
  • 2020-12-04 17:03

    I am using following code to format date and show 2 months in calendar...

    <script>
    $(function() {
    
        var dates = $( "#from, #to" ).datepicker({
    
            showOn: "button",
            buttonImage: "imgs/calendar-month.png",
            buttonImageOnly: true,                           
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 2,
    
            onSelect: function( selectedDate ) {
    
    
    
    
                $( ".selector" ).datepicker({ defaultDate: +7 });
                var option = this.id == "from" ? "minDate" : "maxDate",
    
    
                    instance = $( this ).data( "datepicker" ),
                    date = $.datepicker.parseDate(
                        instance.settings.dateFormat ||
                        $.datepicker._defaults.dateFormat,
                        selectedDate, instance.settings );
    
                dates.not( this ).datepicker( "option", "dateFormat", 'yy-mm-dd' );
    
    
            }
        });
    });
    
    </script>
    

    The problem is I am not sure how to restrict previous dates selection.

    0 讨论(0)
  • 2020-12-04 17:04

    If you are dealing with a previously bound date picker then setting

    $("#datepicker").datepicker({ minDate: 0 });
    

    will not work. That syntax is applicable only when you are creating the widget.

    To set min date for a bound date picker use the following:

    $("#datePicker").datepicker("option", "minDate", 0);
    
    0 讨论(0)
  • 2020-12-04 17:07

    $("#datePicker").datePicker({startDate: new Date() });. This works for me

    0 讨论(0)
提交回复
热议问题