jQuery的响应日历无法设置选定的日期

时间:2019-03-01 08:25:25

标签: jquery calendar

我正在jquery中使用http://w3widgets.com/responsive-calendar中的Web Application响应式压光机。我使用以下代码初始化日历: //初始化响应式压延机

if(jQuery('.responsive-calendar').length)
{
    jQuery('.responsive-calendar').responsiveCalendar({
        format: 'dd/MM/yyyy',
        allRows: false,
        startFromSunday: true,
        onInit: function() {
            if(jQuery("#selectedDay").val().length)
            {
                var currentDate = jQuery("#selectedDay").val();
                var dateSplit = currentDate.split('-');
                //jQuery('.responsive-calendar').responsiveCalendar(dateSplit[2]+"-"+dateSplit[1]);
                jQuery('.responsive-calendar').responsiveCalendar('2014-02');
                jQuery(".responsive-calendar a[data-day=6][data-month=2][data-year=2014]").addClass('selectedDay');
                //jQuery('.responsive-calendar a[data-day=' + dateSplit[0].replace(/^0+/, '') + '][data-month=' + dateSplit[1].replace(/^0+/, '') + '][data-year=' + dateSplit[2] + ']').addClass('selectedDay');
            }
            else
            {
                jQuery('.responsive-calendar .today a').addClass('selectedDay');
            }
        },
        onDayClick: function(events) { 
            jQuery(".alert").hide();
            var thisDayEvent, key;
            var monthNames = new Array("undefined", "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
            key = addLeadingZero(jQuery(this).data('year') + '-' + monthNames[jQuery(this).data('month')] + '-' + addLeadingZero(jQuery(this).data('day')));
            var url = jQuery('#cbrainEntityUrl').val();
            var activity = jQuery('#contentType').val();
            url = url + activity + "/" + key
            window.location.href = url;
        }
    });
}

在页面加载期间也使用以下脚本:

jQuery(window).bind("load", function() { 
//Set current selected day on initial page load
if(jQuery('#selectedDay').length)
{
    var currentDate = 0;
    if(jQuery("#selectedDay").val().length)
    {
        currentDate = jQuery("#selectedDay").val();
        var dateSplit = currentDate.split('-');
        jQuery('.responsive-calendar').responsiveCalendar('2014-02');
        //jQuery('.responsive-calendar a[data-day=' + dateSplit[0].replace(/^0+/, '') + '][data-month=' + dateSplit[1].replace(/^0+/, '') + '][data-year=' + dateSplit[2] + ']').addClass('selectedDay');
    }
    else
    {
        var nowDate = new Date();
        var curr_day = nowDate.getDate();
        curr_day = ("0" + curr_day).slice(-2);
        var curr_month = nowDate.getMonth() + 1;
        curr_month = ("0" + curr_month).slice(-2);
        var curr_year = nowDate.getFullYear();
        currentDate = curr_day + "-" + curr_month + "-" + curr_year;
        jQuery("#selectedDay").val(currentDate);
        jQuery('.responsive-calendar .today a').addClass('selectedDay');
    }
}

});

在控制台中执行以下两行脚本时,将转到指定的月份,并通过添加一个类来突出显示所选的日期:

jQuery('.responsive-calendar').responsiveCalendar('2014-02');
jQuery(".responsive-calendar a[data-day=6][data-month=2][data-year=2014]").addClass('selectedDay');

但是脚本在加载时不会到达指定的日期。我要做的就是将日历设置为指定的日期,并在其中添加一个类,以使其保持选中状态。

0 个答案:

没有答案