Create a specific date range with jQuery datepicker

前端 未结 2 1162
陌清茗
陌清茗 2021-02-06 09:54

I\'m trying to use the jQuery date picker to create a start date calendar and an end date calender. I\'m using the \"date range\" example seen here: http://jqueryui.com/demos/da

2条回答
  •  滥情空心
    2021-02-06 10:38

    This should do it:

    http://jsfiddle.net/abze4/

    $(function() {
        var fromDate = $("#from").datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 2,
            minDate: new Date(),
            onSelect: function(selectedDate) {
                var instance = $(this).data("datepicker");
                var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
                date.setDate(date.getDate()+30);
                toDate.datepicker("option", "minDate", date);
            }
        });
    
        var toDate = $("#to").datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 2
        });
    });
    

    Basically this sets the from date's minDate to today. Then when you select a fromDate, it sets the minDate of the toDate to the selected date +30.

提交回复
热议问题