【问题标题】:TypeError: $(...).autocomplete is not a functionTypeError: $(...).autocomplete 不是函数
【发布时间】:2013-04-30 13:02:17
【问题描述】:

Drupal 模块中使用以下代码时出现错误TypeError: $(...).autocomplete is not a function

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');

【问题讨论】:

  • jQueryUI 加载了吗?
  • 自动完成是一个 jQuery-UI 功能,不是吗?您是否也在为此加载必要的文件?

标签: jquery jquery-ui


【解决方案1】:

你错过了 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>

【讨论】:

  • 不工作,我在 package.json 文件中包含了所有使用 npm 的库。但仍然显示此错误 Uncaught TypeError: $(...).autocomplete is not a function at HTMLInputElement. (index.js:491) at HTMLInputElement.dispatch (jquery.js:5226) at HTMLInputElement.elemData.handle (jquery.js:4878)
  • 刚刚有一个时刻,您希望以后可以选择悬赏回答。谢谢!
【解决方案2】:

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

<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>

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      (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);
      

      【讨论】:

        【解决方案5】:

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

        <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>
        

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

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-06-12
            • 1970-01-01
            • 1970-01-01
            • 2012-06-13
            相关资源
            最近更新 更多