2017-01-02 14 views
2

我使用的日曆從http://w3widgets.com/responsive-calendar/。 我想在onMonthChange上獲取事件的日期。 但是,年,月和日總是未定義的。responsive-calendar:獲取事件onMonth的日期更改

該插件的文檔做得很好,但沒有解釋如何通過示例做到這一點。

這裏是我的事件:

onMonthChange: function(events) { 
     var $year=$(this).data('year'); //here I get undefined 
     var $month=$(this).data('month');//here I get undefined 
     var $day=1; //but I´d like to get the day of the current date after changed 
    },  

如果我使用的文檔這一事件很管用:

onDayClick: function(events) { 
     var thisDayEvent, key; 

     key = $(this).data('year')+'-'+addLeadingZero($(this).data('month'))+'-'+addLeadingZero($(this).data('day')); 
     } 
+0

您是否找到解決此問題的解決方案? – Polo

回答

1

你是顯示文件適用於一天的事件。

使用onMonthChange時,沒有事件參數,並且爲了訪問該對象,請使用$(this)[0]。這使您可以訪問新選定月份通過屬性currentMonthcurrentYear(見代碼):

$(".responsive-calendar").responsiveCalendar({ onMonthChange: function() { 
 
alert(($(this)[0].currentMonth + 1) + '/' + $(this)[0].currentYear); 
 
      } });

請注意,這裏使用的JavaScript得到月(),所以幾個月將是0 -11。