问题
I am looking to restrict the months for the jQuery UI datepicker
.
For example, I want to prevent users from selecting april, august and October in any given year. I have my months in an array of numbers, eg 4,8,10.
回答1:
I hope you tried something, but I have a few spare minutes so f I understand correctly you want to disable the use of some months.
You can use the beforeShowDay event of jQuery UI Datepicker and check if a day is active or not based on its month and check if is in the array.
To check the array I use the jQuery inArray method.
beforeShowDay
Default: null A function that takes a date as a parameter and must return an array with: [0]: true/false indicating whether or not this date is selectable 1: a CSS class name to add to the date's cell or "" for the default presentation 2: an optional popup tooltip for this date The function is called for each day in the datepicker before it is displayed.
jQuery.inArray
Description: Search for a specified value within an array and return its index (or -1 if not found).
Code sample:
monthArray = [0, 3, 6]
$(function () {
$("#datepicker").datepicker({
beforeShowDay: function (date) {
//getMonth() is 0 based
if ($.inArray(date.getMonth(), monthArray) > -1) {
return [false, ''];
}
return [true, ''];
}
});
});
Here is a working fiddle: http://jsfiddle.net/APP5N/
来源:https://stackoverflow.com/questions/17304900/jquery-ui-datepicker-limit-certain-months-not-a-range-of-months