Datepicker dynamic min / max dates

后端 未结 2 734
一向
一向 2021-01-07 06:44

I\'m using the jQuery datepicker plugin on a site I am building. I need users to be able to pick two dates from within a specified range of dates. This is straightforward en

2条回答
  •  旧时难觅i
    2021-01-07 07:31

    If you want to do this, you can use the "beforeShow" attribute of datepicker to help you out. Here is a pseudo-code example.

    // Your start and end datepickers.
    $('#dateStartMainChart, #dateEndMainChartSelect').datetimepicker({
        beforeShow: customRange
    });
    
    // I can't take credit for this...website to tutorial is below.
    // From: http://test.thecodecentral.com/cms/jqueryui/datepicker/
    function customRange(input) {
        return { minDate: (input.id == 'dateEndMainChartSelect' ? $('#dateStartMainChart').datepicker('getDate') : null),
            maxDate: (input.id == 'dateStartMainChart' ? $('#dateEndMainChartSelect').datepicker('getDate') : null)
        };
    }
    

    You don't even need to look at the database directly (although, you can - adjust the fields appropriately in the customRange function). In any case, this should do what you need.

提交回复
热议问题