这太疯狂了,我不知道为什么!

这是我的问题:

当我登录calEvent.start时,我会得到正确的日期和正确的时间,但是!当我登录obj calEvent时,我的开始日期不显示时间:(。

我做:

console.log('=======addNewEvent=========');
console.log('log of calEvent.start : ' + calEvent.start);
console.log('log of calEvent obj : ');
console.dir(calEvent);
console.log('========================');


这就是我得到的:

console.log(calEvent.start)

Fri Oct 25 2013 12:30:00 GMT-0400 (EDT)


console.log(calEvent)

Object
    date: "2013-10-25"
    date_end: "2013-10-25"
    date_end_hour: "6:30 pm"
    date_start_hour: "5:30 pm"
    end: Fri Oct 25 2013 18:30:00 GMT-0400 (EDT)
    end_date: "2013-10-25"
    end_time: "18:30"
    id: 109
    location: "63"
    presettype: null
    repeat_type: "N"
    start: Fri Oct 25 2013 00:00:00 GMT-0400 (EDT)
    start_date: "2013-10-25"
    start_time: "17:30"
    time_zone: 4
    title: "s"
    type: "C"
    typeClass: "class"
    user_locations: null
    zipcode: "10023"


如您所见,calEvent.start将正确的时间显示为12:30:00,而calEvent将时间显示为00:00:00

知道为什么会这样吗? :(

注意:对不起我的英语*

========================= [设置calEvent.start的代码] ================= =========

eventSetEvent : function(calEvent, $event, $titleName, $typeName, $typeL) {
 resetForm($dialogContent);

 $dialogContent.dialog({
    modal: true,
    title: $titleName,
    open: function() {
        $(".j-start-time").timepicker('setTime',  MAIN.calendar.from24To12(MAIN.calendar.getInfoFromDate().hours(calEvent.start) + ':'+ MAIN.calendar.getInfoFromDate().minutes(calEvent.start)) );
        $(".j-end-time").timepicker('setTime',  MAIN.calendar.from24To12(MAIN.calendar.getInfoFromDate().hours(calEvent.end) + ':'+ MAIN.calendar.getInfoFromDate().minutes(calEvent.end)) );
    },
    close: function() {
       $dialogContent.dialog("destroy");
       $('#calendar').weekCalendar("removeUnsavedEvents");
    },
    buttons: {
       save : function() {
            var x = new Date();
                time_zone = x.getTimezoneOffset() / 60;

            calEvent.id = id;
            id++;

            MAIN.calendar.validation();

            if( $typeL == 'C') {
                calEvent.typeClass = 'class';
            }

            if( $typeL == 'P') {
                calEvent.typeClass = 'private';
            }

            if( $typeL == 'O') {
                calEvent.typeClass = 'available';
            }

            if( $('.error-field').doesExist() ) {
                $('.j-err-msg').fadeIn();
            } else {
                $('.j-err-msg').fadeOut();

                calEvent.date = MAIN.calendar.getInfoFromDate().year(calEvent.start) + '-' + MAIN.calendar.getInfoFromDate().month(calEvent.start) + '-' + MAIN.calendar.getInfoFromDate().day(calEvent.start);
                calEvent.date_end = $('.j-date').val() != '' ? $('.j-date').val() : MAIN.calendar.getInfoFromDate().year(calEvent.start) + '-' + MAIN.calendar.getInfoFromDate().month(calEvent.start) + '-' + MAIN.calendar.getInfoFromDate().day(calEvent.start);
                calEvent.date_end_hour = $('.j-end-time').val().toLowerCase();
                calEvent.date_start_hour = $('.j-start-time').val().toLowerCase();
                calEvent.end_date = $('.j-date').val() != '' ? $('.j-date').val() : MAIN.calendar.getInfoFromDate().year(calEvent.start) + '-' + MAIN.calendar.getInfoFromDate().month(calEvent.start) + '-' + MAIN.calendar.getInfoFromDate().day(calEvent.start);
                calEvent.end_time = MAIN.calendar.getInfoFromDate().hours(calEvent.end) + ':' + MAIN.calendar.getInfoFromDate().minutes(calEvent.end);
                calEvent.presettype = null;
                calEvent.repeat_type = $('.j-radio-repeat:checked').val();
                calEvent.start_date = MAIN.calendar.getInfoFromDate().year(calEvent.start) + '-' + MAIN.calendar.getInfoFromDate().month(calEvent.start) + '-' + MAIN.calendar.getInfoFromDate().day(calEvent.start);
                calEvent.start_time = MAIN.calendar.getInfoFromDate().hours(calEvent.start) + ':' + MAIN.calendar.getInfoFromDate().minutes(calEvent.start);
                calEvent.time_zone = time_zone;
                calEvent.title =  $('.j-title').val() ;
                calEvent.type = $typeL;
                calEvent.user_locations = null;
                calEvent.zipcode = $('.j-zipcode').val();

                MAIN.calendar.addNewEvent(calEvent);
                $calendar.weekCalendar("removeUnsavedEvents");
                $calendar.weekCalendar("updateEvent", calEvent);
                $(".ui-dialog-content").dialog("close");
            }
       },
       cancel : function() {
          $(".ui-dialog-content").dialog("close");
       }
    }
 }).show();

 $dialogContent.find(".date_holder").text($calendar.weekCalendar("formatDate", calEvent.start));
},

最佳答案

我怀疑您被JavaScript控制台欺骗了。

当您登录calEvent.start时,它将记录该属性当时的实际值。

当您仅登录calEvent时,它仅显示一行,对吗?然后单击小箭头以展开对象列表,对吗?

然后发生的事情是调试器在扩展对象时(而不是在您首次调用calEvent时)获取console.log(calEvent)对象的属性。

当我有了您时,让我们谈谈如何使您的代码更简单。我仅举一个例子:冗长的函数调用MAIN.calendar.getInfoFromDate()在此函数中出现20次。

除非有特定的原因,否则应该调用一次函数,将其返回值保存在变量中,然后在那20个位置使用该变量:

eventSetEvent : function(calEvent, $event, $titleName, $typeName, $typeL) {
 resetForm($dialogContent);
 var info = MAIN.calendar.getInfoFromDate();

 $dialogContent.dialog({
    modal: true,
    title: $titleName,
    open: function() {
        $(".j-start-time").timepicker('setTime',  MAIN.calendar.from24To12(info.hours(calEvent.start) + ':'+ info.minutes(calEvent.start)) );
        $(".j-end-time").timepicker('setTime',  MAIN.calendar.from24To12(info.hours(calEvent.end) + ':'+ info.minutes(calEvent.end)) );
    },
    close: function() {
       $dialogContent.dialog("destroy");
       $('#calendar').weekCalendar("removeUnsavedEvents");
    },
    buttons: {
       save : function() {
            var x = new Date();
                time_zone = x.getTimezoneOffset() / 60;

            calEvent.id = id;
            id++;

            MAIN.calendar.validation();

            if( $typeL == 'C') {
                calEvent.typeClass = 'class';
            }

            if( $typeL == 'P') {
                calEvent.typeClass = 'private';
            }

            if( $typeL == 'O') {
                calEvent.typeClass = 'available';
            }

            if( $('.error-field').doesExist() ) {
                $('.j-err-msg').fadeIn();
            } else {
                $('.j-err-msg').fadeOut();

                calEvent.date = info.year(calEvent.start) + '-' + info.month(calEvent.start) + '-' + info.day(calEvent.start);
                calEvent.date_end = $('.j-date').val() != '' ? $('.j-date').val() : info.year(calEvent.start) + '-' + info.month(calEvent.start) + '-' + info.day(calEvent.start);
                calEvent.date_end_hour = $('.j-end-time').val().toLowerCase();
                calEvent.date_start_hour = $('.j-start-time').val().toLowerCase();
                calEvent.end_date = $('.j-date').val() != '' ? $('.j-date').val() : info.year(calEvent.start) + '-' + info.month(calEvent.start) + '-' + info.day(calEvent.start);
                calEvent.end_time = info.hours(calEvent.end) + ':' + info.minutes(calEvent.end);
                calEvent.presettype = null;
                calEvent.repeat_type = $('.j-radio-repeat:checked').val();
                calEvent.start_date = info.year(calEvent.start) + '-' + info.month(calEvent.start) + '-' + info.day(calEvent.start);
                calEvent.start_time = info.hours(calEvent.start) + ':' + info.minutes(calEvent.start);
                calEvent.time_zone = time_zone;
                calEvent.title =  $('.j-title').val() ;
                calEvent.type = $typeL;
                calEvent.user_locations = null;
                calEvent.zipcode = $('.j-zipcode').val();

                MAIN.calendar.addNewEvent(calEvent);
                $calendar.weekCalendar("removeUnsavedEvents");
                $calendar.weekCalendar("updateEvent", calEvent);
                $(".ui-dialog-content").dialog("close");
            }
       },
       cancel : function() {
          $(".ui-dialog-content").dialog("close");
       }
    }
 }).show();

 $dialogContent.find(".date_holder").text($calendar.weekCalendar("formatDate", calEvent.start));
},


您可以进行其他简化,但这将为您提供一个起点。

关于javascript - 奇数数组值问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19578070/

10-11 05:25