最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery datepicker - how to change max date on a specific start date? - Stack Overflow

programmeradmin0浏览0评论

I have the following code:

$(function() {          
var dates = $( "#from_date, #to_date" ).datepicker({            
defaultDate: "-1w",
dateFormat: 'yy-mm-dd',
            onSelect: function( selectedDate ) {
                if (selectedDate < "2012-03-27" ) {
                    maxDate: new Date(2012, 03 - 1, 27); // it will set minDate from 25 October 2009
                };
                var option = this.id == "from_date" ? "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 );

            }
        });

    });

I need the maxDate to to be limited to the 2012-03-27, in case the start date was smaller then that.

I tried EVERYTHING....really desperate here thanks, Danny

I have the following code:

$(function() {          
var dates = $( "#from_date, #to_date" ).datepicker({            
defaultDate: "-1w",
dateFormat: 'yy-mm-dd',
            onSelect: function( selectedDate ) {
                if (selectedDate < "2012-03-27" ) {
                    maxDate: new Date(2012, 03 - 1, 27); // it will set minDate from 25 October 2009
                };
                var option = this.id == "from_date" ? "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 );

            }
        });

    });

I need the maxDate to to be limited to the 2012-03-27, in case the start date was smaller then that.

I tried EVERYTHING....really desperate here thanks, Danny

Share Improve this question asked Mar 26, 2012 at 18:20 Danny ValariolaDanny Valariola 1,1285 gold badges26 silver badges41 bronze badges 3
  • Limited in which field ? You are not using maxDate anywhere... – Didier Ghys Commented Mar 26, 2012 at 18:28
  • I don't understand question. Please clarify what you're trying to achieve. – Marc Commented Mar 26, 2012 at 18:44
  • Hi,Please allow me to clearify: when someone select the from_date to be something before the 27.03.2012 i want to to_date to be locked to the 27.03.2012 I tried using "maxDate: new Date(2012, 03 - 1, 27);" but not luck. – Danny Valariola Commented Mar 26, 2012 at 18:48
Add a ment  | 

1 Answer 1

Reset to default 3

Ok I see now what you are trying to do with the maxDate. You are trying to set an option to the datepicker while being in an event handler !

$("#from_date, #to_date").datepicker({
    defaultDate: "-1w",
    dateFormat: 'yy-mm-dd',
    onSelect: function(selectedDate) {
        // begin event handler
        if (selectedDate < "2012-03-27") {
             // you cannot set option of the datepicker like this from
             // an event handler !
             maxDate: new Date(2012, 03 - 1, 27);
        }
        // end of event handler
    }
});

To change an option of a datepicker instance, use the method "option" of the plugin:

$(...).datepicker("option", <optionName>, <newValue>);

Now, i'm still not sure to pletely understand what you want with the dates but to lock the other datepicker on a certain date, you can set the minDate and the maxDate so only this date is selectable:

if (selectedDate < "2012-03-27") {
    var maxDate = new Date(2012, 03 - 1, 27);
    $("#to_date").datepicker("option", 'minDate', maxDate);
    $("#to_date").datepicker("option", 'maxDate', maxDate); 
}          
发布评论

评论列表(0)

  1. 暂无评论