我在jQuery UI 1.8.6中使用了自动完成功能。我想突出显示匹配结果。但由于某些原因,当我使用正则表达式在匹配字符周围添加“强”标签时,字符串正在被转义。所以我看到[strong]matching chars[/strong]
,而不是标记文字。突出显示jQuery UI自动完成
这是我目前使用的JavaScript:
$(function() {
$("#autocompleteinputfield").autocomplete({
source: function (request, response) {
$.ajax({
url: "someservice",
type: "GET",
dataType: "json",
data: { filter: request.term, maxResults: 10 },
success: function (data) {
response($.map(data, function (item) {
// return { label: item.ID + ' - ' + item.Name, id: item.ID, value: item.Name }
var regex = new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + request.term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi");
return { label: (item.ID + ' - ' + item.Name).replace(regex, "<strong>$1</strong>"),
id: item.ID,
value: item.Name
}
}))
}
});
},
select: function (event, ui) {
alert(ui.item ? ("You picked '" + ui.item.label + "' with an ID of " + ui.item.id)
: "Nothing selected, input was " + this.value);
}
});
});
更新:
“输入”(在文本框中输入文本在这种情况下:[输入类型= “文本” 的id = “autocompleteinputfield”/]
输出看起来像这样:
[{ “描述”: “没有什么意义的”, “ID”:3, “名称”: “乔公共”}]
当AJAX数据来自于响应,它是什么样子? – 2010-11-05 15:19:41