2013-03-23 68 views
1

我填充所有月份頁面上的「分裂」,例如:fullCalendar - 遺漏的類型錯誤:無法調用的不確定

<?php foreach(range(0,11) as $month): ?> 
     <div style='float:left; width:303px;' id='calenderSalesMonth_<?php echo $month ?>' class='calenderSales'></div> 
<?php endforeach; ?> 

fullCalendar應通過id='calenderSalesMonth_*'設置特定的月份,它是行不通的,我得到一個錯誤:

Uncaught TypeError: Cannot call method 'split' of undefined

代碼:

$('.calenderSales').fullCalendar({ 
     editable: false, 
     events: { url: 'calender-events.php' }, 
     firstDay:1, 
     month: $(this).attr("id").split("_")[1], 
     year: 2013 
}); 
+0

如果'.attr( 「ID」)'收益不確定的,你不能叫'.split'就可以了,因爲'.split'是一個String對象的方法,不能在未定義時調用。 – 2013-03-23 11:36:40

+0

這個代碼在一些'click'事件處理程序中嗎? – dfsq 2013-03-23 11:36:58

+0

@dfsq不,它不是。 – 2013-03-23 11:39:23

回答

1

用戶month全局變量然後設置它。

var month = $('.calenderSales').attr("id").split("_")[1]; 

$('.calenderSales').fullCalendar({ 
     editable: false, 
     events: { url: 'calender-events.php' }, 
     firstDay:1, 
     month: month, 
     year: 2013 
}); 

OR

使用.prop().attr()替代。

OR

嘗試包裏面的代碼$(window).load(function());

+0

聲明一個全局'月份'變量不會工作,因爲我在頁面上有很多個月,例如:'calenderSales_0','calenderSales_1','calenderSales_2' – 2013-03-23 11:50:41

+0

@ I'll-Be-Back ohh所以你試過了'prop()' – 2013-03-23 11:53:28

+0

升級jQuery,'prop'不會工作。同樣的錯誤。'month:$(this).prop(「id」)。split(「_」)[1],' – 2013-03-23 12:02:35

相關問題