在
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');
你错过了 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>
简单的解决方案: 包含自动完成库时,顺序确实很重要:
<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>
根据我的经验,我在文件中添加了两个 Jquery 库。版本是 jquery 1.11.1 和 2.1。突然我从代码中取出了 2.1 Jquery。然后运行它并为我工作得很好。在尝试了第一个答案之后。请像我上面说的那样检查你的文件。
尝试这段代码,让 $ 被定义
(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);
只需将这些添加到您项目的库中即可:
<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>
保存并重新加载。你可以走了。
就我而言,我从 TAMPERMONKEY 脚本中包含了一个额外的 jquery 库!禁用脚本即可工作。
这些链接不起作用。 我的 View Cdn 是这些。
<!--AutoComplete-->
<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>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>