简体   繁体   English

如何在jQuery datepicker中禁用日期?

[英]How to disable dates in jQuery datepicker?

I have 2 datepicker for filling checkin text box and checkout text box. 我有2个日期选择器,用于填写checkin文本框和checkout文本框。 I have to enable only checkin +13 days in checkout datepicker. 我必须在checkout日期选择器中仅启用checkin +13天。 What I tried is- 我试过的是-

       $("#Chkin").datepicker({ dateFormat:  'dd/mm/yy', minDate: '+0', onClose: function (dateText, inst) {
            if ($("#ctl00_ContentPlaceHolder1_hdnDateformat").val() == "dd/mm/yy") {
                var parts = dateText.split("/");
                var cin = new Date(Number(parts[2]), Number(parts[1]) - 1, Number(parts[0]));
            }
            else {
                var cin = new Date(dateText);
            }
            var cout = new Date(cin.getFullYear(), cin.getMonth(), cin.getDate()+1);           
            $("#Chkout").datepicker('option', 'minDate', cout).datepicker('show');
            showDays();
        } 
        });
        var cin = new Date($("#Chkin").val());
        var cout = new Date(cin.getFullYear(), cin.getMonth(), cin.getDate()+1);  
        var maxOut= new Date(cin.getDate()+13);
        $("#Chkout").datepicker({ dateFormat:  'dd/mm/yy', minDate: cout, maxDate: maxOut, onSelect: showDays });

Where I went wrong? 我哪里出问题了?

I was able to make it work using the following code. 我能够使用以下代码使其工作。 Note that I had to deviate a bit from your pasted code since I did not have access to the showDays function. 请注意,由于我无权访问showDays函数,因此我不得不偏离您粘贴的代码。 Anyway the issue in your code was that while constructing the maxOut date object, you were using var maxOut = new Date(cin.getDate()+13), which would actually construct a date object using cin.getDate()+13 as the given year. 无论如何,代码中的问题是,在构造maxOut日期对象时,您使用的是var maxOut = new Date(cin.getDate()+ 13),实际上将使用cin.getDate()+ 13构造日期对象给定的一年。 I am also setting the maxdate option on the checkout datepicker in the onclose function. 我还在onclose函数的结帐日期选择器中设置了maxdate选项。 See the date constructor arguments at http://www.w3schools.com/js/js_obj_date.asp 请参阅http://www.w3schools.com/js/js_obj_date.asp上的日期构造函数参数

$(function() {

$("#Chkin").datepicker({ dateFormat:  'dd/mm/yy', minDate: '+0', onClose: function (dateText, inst) {
    var cin = $(this).datepicker( "getDate" ); // this returns a date object, or null is nothing is selected.
    if(cin != null && cin != 'Invalid Date'){ // check for a valid date object, definitely can be done in a better way.
        var cout = new Date(cin.getFullYear(), cin.getMonth(), cin.getDate()+1);
        var maxOut = new Date(cin.getFullYear(), cin.getMonth(), cin.getDate()+13);

        $("#Chkout").datepicker('option', 'minDate', cout)
            .datepicker( "option", "maxDate", maxOut )
            .datepicker('show');

    }
} 
}); 


var cin = $("#Chkin").datepicker( "getDate" ); //new Date($("#Chkin").val());

if(cin != null && cin != 'Invalid Date'){ // check for a valid date object, definitely can be done in a better way.
    var cout = new Date(cin.getFullYear(), cin.getMonth(), cin.getDate()+1);  
    var maxOut= new Date(cin.getFullYear(), cin.getMonth(), cin.getDate()+13); 
    $("#Chkout").datepicker({ dateFormat:  'dd/mm/yy', minDate: cout, maxDate: maxOut});

}else{
    $("#Chkout").datepicker({ dateFormat:  'dd/mm/yy'});
}


}); 

I could sorted it finally. 我终于可以排序了。 Here is the code- 这是代码-

$("#Chkin").datepicker({ dateFormat:  $("#ctl00_ContentPlaceHolder1_hdnDateformat").val(), minDate: '+0', onClose: function (dateText, inst) {
            if ($("#ctl00_ContentPlaceHolder1_hdnDateformat").val() == "dd/mm/yy") {
                var parts = dateText.split("/");
                var cin = new Date(Number(parts[2]), Number(parts[1]) - 1, Number(parts[0]));
            }
            else {
                var cin = new Date(dateText);
            }
            var cout = new Date(cin.getFullYear(), cin.getMonth(), cin.getDate()+1); 
            var maxOut= new Date(cin.getFullYear(), cin.getMonth(), cin.getDate()+14);
            $("#Chkout").datepicker('option', 'minDate', cout);
            $("#Chkout").datepicker('option', 'maxDate', maxOut);
            showDays();
        } 
        });
        var cin = new Date($("#Chkin").val());
        var cout = new Date(cin.getFullYear(), cin.getMonth(), cin.getDate()+1);   
        var maxOut= new Date(cin.getFullYear(), cin.getMonth(), cin.getDate()+14);
        $("#Chkout").datepicker({ dateFormat:  $("#ctl00_ContentPlaceHolder1_hdnDateformat").val(), minDate: cout, maxDate: maxOut, onSelect: showDays });

What I the changes i made are called the function option like this- 我所做的更改被称为函数option例如:

            $("#Chkout").datepicker('option', 'minDate', cout);
            $("#Chkout").datepicker('option', 'maxDate', maxOut);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM