【发布时间】:2010-03-11 10:07:22
【问题描述】:
我有一个 jquery datepicker,它默认将当前日期呈现为一个完整的日历。在呈现之前,我通过 ajax 从服务器获取了当月需要突出显示的天数列表。代码如下:
$.get("Note/GetActionDates/?orgID=" + orgID + "&month=" + month +"&year=" + year,
null, function(result) {
RenderCalendar(result);
}, "json");
function RenderCalendar(dates) {
$("#actionCal").datepicker({ dateFormat: 'dd/mm/yy', beforeShowDay: function(thedate) {
var theday = thedate.getDate();
if ($.inArray(theday, dates) == -1) {
return [true, "", ""];
}
else {
return [true, "specialDate", "Actions Today"];
}
}
});
}
这一切都很好,但我希望在用户点击不同月份时更新突出显示的日期。我可以使用以下代码修改 jquery datepicker 初始化代码:
onChangeMonthYear: function(year, month, inst) {
//get new array of dates for that month
$.get("Note/GetActionDates/?orgID=" + orgID + "&month=" + month + "&year=" + year,
null, function(result) {
RenderCalendar(result);
}, "json");
}
但这似乎不起作用。
谁能告诉我我做错了什么?谢谢! :)
更新 - 工作代码
感谢您的帮助!
我已经对 petersendidit 中的代码进行了如下调整,现在它可以工作了。将添加更多代码以从日期数组中删除重复的日期,但除此之外一切都很好。
$("#actionCal").datepicker({
dateFormat: 'dd/mm/yyyy',
beforeShowDay: function(thedate) {
var theday = thedate.getDate() + "/" + (thedate.getMonth() + 1) + "/" + thedate.getFullYear();
if ($.inArray(theday, actionCalDates) == -1) {
return [true, "", ""];
} else {
return [true, "specialDate", "Actions Today"];
}
},
onChangeMonthYear: function(year, month, inst) {
dateCount = 0;
getDates(orgID, month, year);
}
});
function getDates(orgID, month, year) {
dateCount += 1;
if (dateCount < 4) {
$.ajax({
url: "Note/GetActionDates/",
data: {
'orgID': orgID,
'month': month,
'year': year
},
type: "GET",
dataType: "json",
success: function(result) {
actionCalDates = actionCalDates.concat(result);
getDates(orgID, month + 1, year);
getDates(orgID, month - 1, year);
}
});
}
}
【问题讨论】: