2013-10-22 46 views
4

我正在使用Yii Framework來開發應用程序。我使用

$(document).ready(function(){ 

    $('.date-picker').datepicker(); 

}); 

無處不在運行,但是當我把它放在index.php的視圖文件,那麼它給我在Firefox控制檯以下錯誤:

TypeError: $(...).datepicker is not a function

$('.date-picker').datepicker();

我有搜索上述錯誤無處不在,但沒有解決方案適用於我的標準從stackoverflow和其他博客爲這個查詢。

感謝

+2

datepicker插件JS不包含或有一個不好的路徑。 – ahren

+2

確保你已經包含了datepicker的庫? – Sarath

回答

4

嘗試在您的視圖頁面中包含jQuery庫文件。兩週前我遇到了同樣的問題,但是當我單獨稱呼它時,我的問題就解決了。

<?php 
Yii::app()->clientScript->registerCoreScript('jquery'); 
Yii::app()->clientScript->registerCoreScript('jquery.ui'); 

Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl .'/js/jquery.ui.datepicker.js'); ?> 
2

你的錯誤的來源是最有可能的noConflict()設置的jQuery。下面是從Wordpress Function Reference推理:

The jQuery library included with WordPress is set to the noConflict() mode (see wp-includes/js/jquery/jquery.js). This is to prevent compatibility problems with other JavaScript libraries that WordPress can link.

In the noConflict() mode, the global $ shortcut for jQuery is not available.

你只需要替換$jQuery,你的代碼將工作。

試試這個:

jQuery(document).ready(function() { 
    jQuery('.date-picker').datepicker(); 
}); 

或者,如果你通過它的功能,這樣你可以繼續使用$

jQuery(document).ready(function($) { 
    $('.date-picker').datepicker(); 
}); 
+0

我想這不可能是實際的問題。 – Praveen

+0

在這裏問這個問題之前,我已經完成了這個。我已經使用noconflict和jQuery而不是「$」,但沒有效果:( – Asad

+0

對不起,沒有幫助。 – Saran

相關問題