jQuery UI Datepicker 仅启用数组中的特定日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7709320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery UI Datepicker enable only specific days in array
提问by Anagio
I am trying to disable all dates in a datepicker and only enable dates which are in an array. This is the code I have so far http://jsfiddle.net/peter/yXMKC/the problem is only May 14th shows up as enabled. The others are all disabled. Any ideas?
我试图禁用日期选择器中的所有日期,只启用数组中的日期。这是我到目前为止的代码http://jsfiddle.net/peter/yXMKC/问题仅在 5 月 14 日显示为已启用。其他都是残疾人。有任何想法吗?
var availableDates = ["9-5-2011","14-5-2011","15-5-2011"];
function available(date) {
dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
if ($.inArray(dmy, availableDates) == 1) {
return [true, "","Available"];
} else {
return [false,"","unAvailable"];
}
}
$('#date').datepicker({ beforeShowDay: available });
回答by Jayendra
$.inArray(dmy, availableDates) returns the index of the element, so when you compare with 1 only 14-5-2011 will match. Check for not equal to -1. Should work.
$.inArray(dmy, availableDates) 返回元素的索引,因此当您与 1 比较时,只有 14-5-2011 会匹配。检查不等于-1。应该管用。
Fiddle - http://jsfiddle.net/yXMKC/4/
小提琴 - http://jsfiddle.net/yXMKC/4/
var availableDates = ["9-5-2011","14-5-2011","15-5-2011"];
function available(date) {
dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
console.log(dmy+' : '+($.inArray(dmy, availableDates)));
if ($.inArray(dmy, availableDates) != -1) {
return [true, "","Available"];
} else {
return [false,"","unAvailable"];
}
}