我想在文本字段上使用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