在html页面中有一个javascript文件,包含此内容
var eventsDates = {
"0" :["11-10-2012","21-10-2012","28-10-2012","25-11-2012","15-12-2012"],
"10":["11-10-2012"],
"11":["21-10-2012","28-10-2012"],
"12":["25-11-2012","15-12-2012"]
}
然后通过选择我选择ID只获得几个日期
$('#select_events').bind('change', function(){
eventsDates = $(eventsDates[$(this).val()]);
});
最后,使用此函数构建日期并将它们传递给datepicker插件
function available(date) {
dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
if ($.inArray(dmy, eventsDates) != -1) {
return [true, "","Available"];
} else {
return [false,"","unAvailable"];
}
}
激活datepicker我用这个
$('#datepickerEventi').datepicker({
beforeShowDay:available,
dateFormat: 'dd/mm/yy'
});
问题是,如果我选择另一个值,它第一次工作就不再有效,就好像数组是空的一样
提前致谢
答案 0 :(得分:0)
数组实际上是空的,因为你在这里覆盖了值:
$('#select_events').bind('change', function(){
eventsDates = $(eventsDates[$(this).val()]);
});
使用另一个变量名来缓存所选事件的日期数组。您可以将其更改为:
// default the event dates array to the first option from events dropdown
var selectedEventDates = eventsDates[$("#select_events option:first").val()];
$('#select_events').bind('change', function(){
selectedEventDates = eventsDates[$(this).val()];
});
并在此更新名称:
if ($.inArray(dmy, selectedEventDates) != -1) {