我正在尝试使用_renderItem函数创建一个自定义的ui-menu-item元素但是在尝试之后我甚至无法调用该函数。自动完成工作正常,但它就像_renderItem函数不存在。这是我的脚本scction
<script language="Javascript" type="text/javascript">
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$j(document).ready(function() { //START of ready function
$j( "#custom-report" )
.autocomplete({
source: function( request, response ) {
$j.getJSON( "<?=$this->url(array("controller"=>"report", "action"=>"custom-autocomplete"))?>", {
term: extractLast( request.term )
}, response );
},
search: function() {
//Place holder
},
focus: function (event, ui) {
// Prevent the default focus behavior.
event.preventDefault();
},
select: function( event, ui ) {
var terms = split( this.value );
terms.pop();
terms.push( ui.item.value );
this.value = terms.join( ", " );
return false;
}
}).data("autocomplete")._renderItem = function (ul, item) {
return $("<li />")
.data("item.autocomplete", item)
.append("This is the text")
.addClass("tip")
.attr("desc", "This is the description")
.appendTo(ul);
};
}); //END of ready function
</script>
任何人都知道为什么这不起作用?
答案 0 :(得分:4)
我最终不得不这样做
$.ui.autocomplete.prototype._renderItem = function (ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.addClass("tip ui-menu-item")
.append("<a>" + item.label + "</a>")
.attr("desc", item.description) /* This is the filed that started the whole thing */
.attr("role", "presentation")
.appendTo(ul);
};
答案 1 :(得分:3)
这取决于jQuery UI版本,在较新版本中,对象模型已更改(请参阅:http://jqueryui.com/upgrade-guide/1.10/#autocomplete)。
jQuery UI站点上的示例基于jQuery UI 1.10。
1.9和次要:
.data("autocomplete")._renderItem = function (ul, item) {
return $("<li />")
.data("item.autocomplete", item)
.append("This is the text")
.addClass("tip")
.attr("desc", "This is the description")
.appendTo(ul);
};
1.10和下一个:
.data("ui-autocomplete")._renderItem = function (ul, item) {
return $("<li />")
.data("ui-autocomplete-item", item)
.append("This is the text")
.addClass("tip")
.attr("desc", "This is the description")
.appendTo(ul);
};