jQuery DatePicker日历未返回正确的月份 [英] jQuery DatePicker calendar not returning correct month
问题描述
我在这儿太近了.我希望上师可以帮助我完成最后一部分.我正在使用RSS提要中的XML填充jQuery DatePicker日历.单击出现事件的突出显示的日期后,我将创建一个带有查询字符串的URL,以便可以显示点击日的所有事件.一切正常...直到我单击上个月或下个月更改月份.我的脚本将返回正确的日期,但仅返回当前月份.例如,我导航到May并单击5号,我的URL将是events.htm?month = june& day = 5.关于为什么它不返回我选择的月份的任何想法?这是我的代码:
I am soooo close here. I'm hoping a guru can help me with this last piece. I'm populating a jQuery DatePicker calendar with XML from an RSS feed. Upon clicking a highlighted date where there's an event, I'm creating a URL with a query string so I can display all the event for the clicked day. Everything is working... until I change the month by clicking previous or next month. My script will return the correct day, but is only returning the current month. For example, I navigate to May and click the 5th, my URL will be events.htm?month=june&day=5. Any ideas on why it will not return my selected month? Here's my code:
var data = $.ajax({
url: "news.xml",
type: "GET",
dataType: "xml",
async:false,
success: function(xml){
return xml;
}
} ).responseText;
$(document).ready(function() {
var events = getSelectedDates();
$("div.datepicker").datepicker({
beforeShowDay: function(date) {
var result = [true, '', null];
var matching = $.grep(events, function(event) {
return event.published.getDate() === date.getDate() && event.published.getMonth() === date.getMonth() && event.published.getFullYear() === date.getFullYear();
});
if (matching.length) {
result = [true, 'highlight', null];
}
return result;
},
onSelect: function(dateText) {
var date, selectedDate = new Date(dateText),
i = 0,
event = null;
while (i < events.length && !event) {
date = events[i].published;
if (selectedDate.getFullYear() === date.getFullYear() &&
selectedDate.getMonth() === date.getMonth() &&
selectedDate.getDate() === date.getDate()) {
event = events[i];
}
i++;
}
if (event) {
var eMonNum = event.published.getMonth();
var d = new Date();
var eMonNum = new Array();
eMonNum[0] = "january";
eMonNum[1] = "february";
eMonNum[2] = "march";
eMonNum[3] = "april";
eMonNum[4] = "may";
eMonNum[5] = "june";
eMonNum[6] = "july";
eMonNum[7] = "august";
eMonNum[8] = "september";
eMonNum[9] = "october";
eMonNum[10] = "november";
eMonNum[11] = "december";
var eMon = eMonNum[d.getMonth()];
var eDay = event.published.getDate();
window.location = "events.htm?month="+eMon+"&day="+eDay;
}
}
});
});
function getSelectedDates() {
return $(data).find('entry').map(function() {
return {
title: $('title', this).text(),
published: new Date($('published', this).text())
};
}).get();
}
推荐答案
发现了问题,当您从资源中复制此列表时,您忘记了替换变量.
Found the problem, when you copied this list from a resource, you forgot to replace the variables.
更改(在月末列表中)
var eMon = eMonNum[d.getMonth()];
收件人:
var eMon = eMonNum[event.published.getMonth()];
我所做的就是摆脱坏变量,然后将您的month变量重新分配给您当天使用的变量.您也可以删除d变量的声明,因为不需要它.
All I did was get rid of the bad variable and reassigned your month variable to the one you used for the day. You can also remove the declaration of the d variable, as you will not need it.
祝你好运!
这篇关于jQuery DatePicker日历未返回正确的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!