Rao*_*oul 6 css jquery jquery-ui jquery-autocomplete
我正在使用以下代码,它正在工作,获取值等,但<b> and <br>标签显示为文本而不是呈现.我想要item.id和item.label不同的线,如果可能的话,item.id大胆:
$( "#predictSearch" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "index.pl",
dataType: "json",
data: {
term: request.term
},
success: function( data ) {
response( $.map( data.items, function( item ) {
return {
label: '<B>' + item.id + '</B><br>' + item.label,
value: item.id
}
}));
}
});
},
minLength: 2
});
Run Code Online (Sandbox Code Playgroud)
看起来你有一些额外的代码(ajax调用),它可能不需要自动完成.但是,您可以换掉jquery放入的特殊字符来转义自动完成的"打开"事件中的html.
$("#autocomplete_field").autocomplete({
source: "autocomplete.php",
minLength: 2,
open: function(event, ui){
$("ul.ui-autocomplete li a").each(function(){
var htmlString = $(this).html().replace(/</g, '<');
htmlString = htmlString.replace(/>/g, '>');
$(this).html(htmlString);
});
}
});
Run Code Online (Sandbox Code Playgroud)
完整示例http://www.jensbits.com/2011/03/03/jquery-autocomplete-with-html-in-dropdown-selection-menu/.
而且,如果您在自动完成中使用perl,http://www.jensbits.com/2011/05/09/jquery-ui-autocomplete-widget-with-perl-and-mysql/就是一个例子.
而不是成功事件,使用_renderItem事件.
在Vroom现场实施.输入机场,你会注意到左边的图像.
注意:_renderItem下面有一些复杂的计算.不要那样做,只是利用这个想法.
$("#myInput")
.autocomplete({
minLength: 0,
delay: 10,
source: function (req, responseFn) {
//Your ajax call here returning only responseFn Array having item.id and item.id
},
select: function (event, ui) {
//action upon selecting an item
return false;
}
})
.data("autocomplete")
._renderItem = function (ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a><span class='airoplane'>" + (item[0] + (item[2] == "" ? "" : ", " + item[2]) + (item[1] == "" ? "" : " (" + item[1] + ")")).replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(this.term).replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<span class='highlight'>$1</span>") + "</span></a>")
.appendTo(ul);
};
Run Code Online (Sandbox Code Playgroud)