问题
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