开发者

JQuery auto-complete formatting by putting HTML in "label" response

I'm using JQuery auto-complete and sending a response with "label" and "value". I'm trying to get the label to have formatted HTML. The json_encode escapes the html formatting I'm trying to pass through.

Should I do this differently? How can make json_encode not escape the html formatting (it changes "/" to "\/")?

Example code:

$return[$i]['label'] = $row['t开发者_如何学运维ext'];
$return[$i]['label'] = "<span style='font-weight:bold;'>" . $row['text'] . "</span> - " . row['name'];  

json_encode($return);

and when I just check the url response manually, I can see he's escaping the "" to "<\/span>"


I am not sure which auto complete you are using. If you are using http://jqueryui.com/demos/autocomplete/, your problem is not json the label doesn't support html by default

The label is always treated as text, if you want the label to be treated as html you can use Scott González' html extension. The demos all focus on different variations of the source-option - look for the one that matches your use case, and take a look at the code

You can do something like this. What the code is doing is showing the autocomplete matching text in yellow.

function autoCompleteRender(ul, item) {
    var searchTerm = this.term;
    var itemLabel = item.label;
    itemLabel = itemLabel.replace(new RegExp("(" + searchTerm + ")", "gi"), '<strong class="itemhover">$1</strong>');
    return $("<li></li>").data("item.autocomplete", item).append("<a>" + itemLabel + "</a>").appendTo(ul);
}


$(yourselector).autocomplete({
        source: function(request, response) {
            //your source
        }
    }).data("autocomplete")._renderItem = autoCompleteRender;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜