Disable specific months JqueryUI datepicker

ぐ巨炮叔叔 提交于 2019-12-11 07:47:49

问题


I'm creating a booking form that includes jQuery UI Datepicker, now i have a main issue that I need help with: Some trips will have only certain days active, and can only be sold certain months because, for example cyclone season.

The function to disable specifc weekdays works perfect, but i'm not sure how can I deactivate complete months for example October. Is it possible?

Any advices will be helpful, here is my code, please let me know if should I explain better my question, or if should I add some other parts of my code. Thanks in advance!

$.datepicker.setDefaults( $.datepicker.regional[ getLanguage() ] );

$( "#booking" ).datepicker({
   showOn: "button",
   dateFormat : 'yy/mm/dd',
   buttonText: "Select",
   beforeShowDay: disableSpecificWeekDays,
   changeMonth: true,
   changeYear: true,
   minDate: 0
}
);

// 0 = monday, 1 = tuesday, 2 = wednesday, 3 = thursday, 4=friday, 5 = saturday, 6=sunday
var daysToDisable = [1, 4, 6];

function disableSpecificWeekDays(date) {
            var day = date.getDay();
            for (i = 0; i < daysToDisable.length; i++) {
                if ($.inArray(day, daysToDisable) != -1) {
                    return [false];
                }
            }
            return [true];
 }

With this code for example i'm being able to deactivate specific dates, but to deactivate a whole month i'll need to write each date, and in certain cases, i'll need to deactivate at least three or four months for each trip.

var $disableMonths = new Array("2013/10/01","2013/10/02","2013/10/03", "...");

function disableSpecificMonths(mydate){
var $return = true;
var $returnclass ="available";

$checkdate = $.datepicker.formatDate('yy/mm/dd', mydate);

    for(var i = 0; i < $disableMonths.length; i++) {
            if($disableMonths[i] == $checkdate) {
                $return = false;
                $returnclass= "unavailable";
            }
    }

return [$return,$returnclass];

}

But also, if it's not possible to deactivate a complete month i'll love to get some ideas on how can I mix this two functions, so i'll be deactivating specific Week Days and Specific Dates.


回答1:


You can update your "disable function" to something like this:

var daysToDisable = [1, 4, 6];
var monthsToDisable = [9];

function disableSpecificWeekDays(date) {
    var day = date.getDay();
    if ($.inArray(day, daysToDisable) != -1) {
        return [false];
    }

    var month = date.getMonth();
    if ($.inArray(month, monthsToDisable) != -1) {
        return [false];
    }
    return [true];
}


来源:https://stackoverflow.com/questions/18393441/disable-specific-months-jqueryui-datepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!