How do I set Min Date in Datepicker from another Datepicker?

后端 未结 2 970
故里飘歌
故里飘歌 2021-01-06 06:20

I am currently adding validation to my date pickers and am having trouble setting the min date in the to date picker to be whatever was chosen in the

相关标签:
2条回答
  • 2021-01-06 06:32

    Please check this working fiddle

    JSFiddle

    Use option to set the minDate to the second datepicker

    $("#from").datepicker({
            defaultDate: new Date(),
            minDate: new Date(),
            onSelect: function(dateStr) 
            {         
                $("#to").val(dateStr);
                $("#to").datepicker("option",{ minDate: new Date(dateStr)})
            }
        });
    
    0 讨论(0)
  • 2021-01-06 06:32

    The answer is good but you need to destroy the datepicker of the $to because if not the $to datepicker have still the first date what you select, and remove the option in $to datepicker, this work for me, you not need to make other function for $to.

    $("#from").datepicker({
        defaultDate: new Date(),
        minDate: new Date(),
        onSelect: function(dateStr) 
        {         
            $("#to").datepicker("destroy");
            $("#to").val(dateStr);
            $("#to").datepicker({ minDate: new Date(dateStr)})
        }
    });
    
    0 讨论(0)
提交回复
热议问题