31

TypeError: $(...).autocomplete is not a function在模块中使用以下代码时出现错误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');
4

6 回答 6

79

你错过了 jquery ui 库。使用 Jquery UI 的 CDN,或者如果您想要在本地使用,然后从Jquery Ui下载文件

<link href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" rel="Stylesheet">
<script src="YourJquery source path"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js" ></script>
于 2013-04-30T13:07:48.727 回答
4

简单的解决方案:在包含自动完成库时,顺序确实很重要:

<link href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" rel="Stylesheet">
<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>
于 2017-03-08T06:55:30.767 回答
3

根据我的经验,我在我的文件中添加了两个 Jquery 库。版本是 jquery 1.11.1 和 2.1。突然我从我的代码中取出了 2.1 Jquery。然后运行它并为我工作得很好。在尝试了第一个答案之后。请像我上面所说的那样检查您的文件。

于 2016-11-10T12:46:10.733 回答
1

只需将这些添加到您的项目的库中:

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

保存并重新加载。你可以走了。

于 2019-09-03T07:07:31.390 回答
1

试试这个代码,让 $ 被定义

(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);
于 2019-09-03T12:33:54.380 回答
0

就我而言,我从 TAMPERMONKEY 脚本中包含了一个额外的 jquery 库!禁用脚本并且它可以工作。

于 2021-10-06T03:12:40.973 回答