TypeError:$(...)。autocomplete不是一个函数

时间:2013-04-30 13:02:17

标签: jquery

我在Drupal模块中使用以下代码收到上述错误。

jQuery(document).ready(function($) {
        $("#search_text").autocomplete({
            source:results,
            minLength:2,
            position: { offset:'-30 0' },  
            select: function(event, ui ) { 
                    goTo(ui.item.value);
                    return false;
            }        
    }); 
});

Jquery肯定是加载的,我尝试过使用另一个变量$ - 任何想法还有什么可能是问题?

(编辑)自动完成的Drupal特定答案:

drupal_add_library('system', 'ui.autocomplete');

5 个答案:

答案 0 :(得分:56)

你错过了jquery ui库。使用Jquery UI的CDN或者如果您想在本地使用它,请从Jquery Ui

下载文件
<link href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" rel="Stylesheet"></link>
<script src="YourJquery source path"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js" ></script>

答案 1 :(得分:3)

简单的解决方案: 在包含自动完整库时,序列非常重要:

<link href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" rel="Stylesheet"></link>
<script src='https://cdn.rawgit.com/pguso/jquery-plugin-circliful/master/js/jquery.circliful.min.js'></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js" ></script>

答案 2 :(得分:2)

在我的经验中,我在我的文件中添加了两个Jquery库。版本是jquery 1.11.1和2.1.Suddenly我从我的代码中取出了2.1 Jquery。然后运行它,并为我工作。在尝试第一个答案后。请查看你上面说的文件。

答案 3 :(得分:0)

只需将这些添加到项目的库中即可

<link href="https://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.min.css" rel="stylesheet"></link>
<script src="https://code.jquery.com/ui/1.10.2/jquery-ui.min.js"></script>

保存并重新加载。你很好走。

答案 4 :(得分:0)

尝试此代码,让$定义

(function ($, Drupal) {

  'use strict';

  Drupal.behaviors.module_name = {
    attach: function (context, settings) {
        jQuery(document).ready(function($) {
      $("#search_text").autocomplete({
          source:results,
          minLength:2,
          position: { offset:'-30 0' },  
          select: function(event, ui ) { 
                  goTo(ui.item.value);
                  return false;
          }        
        }); 
        });
   }
  };
})(jQuery, Drupal);