jquery datepicker 设置心态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16368534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:01:36  来源:igfitidea点击:

jquery datepicker set mindate

javascriptjquery

提问by Keith Power

I have two jQuery datepickers to select a fromand todates. I have the following code that if you select the 15th June in the first then in the second you now can only select from the 15th onwards.

我有两个jQuery的datepickers选择日期。我有以下代码,如果您在第一个中选择 6 月 15 日,那么在第二个中您现在只能从 15 日开始选择。

The issue I have is I really need to set the todatepicker to +1 day. So the user could only select 16th onwards.

我遇到的问题是我真的需要日期选择器设置为 +1 天。所以用户只能选择第 16 个。

My skills are not advanced enough to add the date to the parseDate

我的技能不够先进,无法将日期添加到 parseDate

    $(function() {
        var dates = $( "#from_date, #to_date" ).datepicker({
        dateFormat: 'dd-mm-yy',
        minDate: 0,
        onSelect: function( selectedDate ) {
            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", option, date );
            }
    });
    }); 

回答by adeneo

To limit one datepicker based on the other, one can set the minDateand maxDatesettings to the date selected in the other datepicker etc.

要根据另一个日期选择器限制一个日期选择器,可以将minDatemaxDate设置设置为另一个日期选择器中选择的日期等。

Something like this

像这样的东西

$(function() {

     /* global setting */
    var datepickersOpt = {
        dateFormat: 'dd-mm-yy',
        minDate   : 0
    }

    $("#from_date").datepicker($.extend({
        onSelect: function() {
            var minDate = $(this).datepicker('getDate');
            minDate.setDate(minDate.getDate()+2); //add two days
            $("#to_date").datepicker( "option", "minDate", minDate);
        }
    },datepickersOpt));

    $("#to_date").datepicker($.extend({
        onSelect: function() {
            var maxDate = $(this).datepicker('getDate');
            maxDate.setDate(maxDate.getDate()-2);
            $("#from_date").datepicker( "option", "maxDate", maxDate);
        }
    },datepickersOpt));
});