简体   繁体   中英

jQuery datepicker onChangeMonthYear doesn't fire on current month

I'm using the datepicker inline, highlighting the dates that has events. Every time the month changes, it has to load the new events from the server. The problem is, the onChangeMonthYear only fires on months different from the current month. For instance, if the is august, it fires on all previous months (july, june, may...) and all following months (september, october...), but not when I change back to august. This is my code:

function highlightDates(date) {
    for ( var i = 0; i < agenda.length; i++) {
        var start = agenda[i].start.toShortDate();
        var end = agenda[i].end.toShortDate();

        if (start >= date && end <= date) {
            return [ true, 'dateHasEvent' ];
        }
    }

    return [ true, '' ];
}

Date.prototype.toShortDate = function() {
    var date = new Date(this.getFullYear(), this.getMonth(), this.getDate());
    return date;
};

function monthChanged(year, month, instance) {
    var date = new Date(year, month - 1);

    $.getJSON('json.do', {
        time : date.getTime()
    }, updateCalendars);
}

function updateCalendars(data, status) {
    agenda = data;
    $('#calendar').datepicker('refresh');
}

var agenda = [];

$(function() {    
    $('#calendar').datepicker( {
        beforeShowDay : highlightDates,
        onChangeMonthYear : monthChanged,
        changeMonth : false,
        changeYear : false,
        showButtonPanel : false
    });
});

Does anybody know if this is a bug or expected behaviour?

Well, that was stupid on my part. The problem was the getJSON method. It is apparently very picky about what json it accepts. So after I produced correct json, everything worked as it should.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM