我想在文本字段上使用jquery自动完成,以允许用户选择多个标记,如this example,但我得到错误Uncaught TypeError: Cannot read property 'autocomplete' of undefined
。这是导致问题的代码,从链接中复制。
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
<script type="text/javascript">
var tags = [<?php
foreach($this->tags as $tag){
echo "\"{$tag['name']}\",";
}
?>""];
$("textarea:[name='tags']").autocomplete({
minLength: 0,
source: function(request, response) {
// delegate back to autocomplete, but extract the last term
alert($.ui);
$.noConflict();
alert($.ui);
response($.ui.autocomplete.filter(
tags, extractLast(request.term)));
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function(event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(", ");
return false;
}
});
错误来自源内部的行response($.ui.autocomplete.filter(
。出于某种原因,$("textarea:[name='tags']").autocomplete
是完全正确的,但$ .ui.autocomplete不是。
我有jQuery和jQuery UI的都包括在此上面的脚本。什么可能导致这个?
编辑:
我发现,如果我把alert($.ui);
外面显示[Object object]
,自动完成功能,但如果我把相同的警报自动完成的源属性里面,它是不确定的。用$做同样的事情显示$是内部定义的,但$ .ui不是。我在页面上也有一个日期选择器,它显示了同样的事情,$ .ui没有在插件内部定义。为什么jQuery UI在这些插件里面没有定义?
编辑2:
把$.noConflict()
源属性正上方的响应函数得到它工作得足够长的时间对我来说,一个标签类型,那么它崩溃的自动完成,虽然日期选择仍然有效。我试着把它放在创建函数中,所以它只会执行一次,但这只是禁用页面上的所有其他jQuery。
我不是非常有经验与jQuery或Javascript,但它似乎像了jQuery UI插件删除别名。
你确定你有jQuery吗?你确认脚本加载正确吗?添加'alert($)'还会给出'undefined'吗? – JJJ 2012-08-07 17:58:33
在控制台中返回$(“textarea:[name ='tags']”)命令? – dizpers 2012-08-07 17:59:25
'textarea:[name ='tags']'存在吗? – 2012-08-07 18:11:39