[英]jQuery ui autocomplete - renderItem
我正在使用_renderItem來修改結果列表
.data( "autocomplete" )._renderItem = function( ul, item ) {
var temp = item.url.substring(16, item.url.length)
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.value + "<br>" + item.url + "<br>" + item.description + "<br>" + "Support URL: " + item.support_url + "<br>" + "Contact: " + "<a href=" + item.contact + ">Test</a>" + "<br />" + "</a>" )
.appendTo( ul )
這具有自動標記任何看起來像url作為href的行為的行為。 我想將整個項目作為鏈接
在較舊的自動完成中,這樣做:
.result(function(event, item) {
location.href = item.url;
});
但是這不再需要支持了。
有誰知道我怎么做:
1)使用類似於.result函數的東西,只需將整個項目作為鏈接
要么
2)修改_renderItem,使其不會自動將看起來像URL的字符串轉換為href
謝謝。
看來,這在以前的版本中已經發生了變化。 現在你必須使用
$element.data('uiAutocomplete')._renderItem()
自定義jQuery自動完成的更好方法是使用小部件創建自己的擴展版本 。
$.widget( "custom.mySpecialAutocomplete", $.ui.autocomplete, {
// Add the item's value as a data attribute on the <li>.
_renderItem: function( ul, item ) {
return $( "<li>" )
.attr( "data-value", item.value )
.append( $( "<a>" ).text( item.label ) )
.appendTo( ul );
},
// Add a CSS class name to the odd menu items.
_renderMenu: function( ul, items ) {
var that = this;
$.each( items, function( index, item ) {
that._renderItemData( ul, item );
});
$( ul ).find( "li:odd" ).addClass( "odd" );
}
});
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$('#myElement').mySpecialAutocomplete({
source: availableTags
});
定義自動完成時,使用select函數創建鏈接:
$('selector').autocomplete({
source: ...,
select: function(event, ui) { window.location = ui.url; }
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.