简体   繁体   English

jQuery自动完成功能显示不需要的动态html标签

[英]jQuery autocomplete is showing unwanted dynamic html tags

Not sure how these are not being converted to html objects. 不确定如何将它们转换为html对象。

I have this function : 我有这个功能:

var findPattern = new RegExp(request.term.toLowerCase(), "ig");
var highlightMatch = function(match) {
  return '<span class="highlight">' + match + '</span>';
};

And it gets used here : 它在这里使用:

source: function(request, response) {
  $autocomplete_xhr = $.ajax({
    // .. truncated for your viewing pleasure ..
    success: function() {
      return {
         label: $.string(label).interpolate({name: row.customer.name, address: (row.customer.addr == null) ? '' : row.customer.addr}).str.replace(findPattern, highlightMatch),

But the result is I can see the <span> tags instead of them being parsed as HTML. 但是结果是我可以看到<span>标记,而不是将其解析为HTML。

Any ideas why this is happening, and what I can do to remedy this? 有什么想法为什么会发生这种情况,以及我可以采取什么措施来解决这个问题?

Got it! 得到它了! Added this to the end of my Autocomplete code.. Which is to my understand the exact same code that is included in the JS library. 将此添加到我的自动完成代码的末尾。据我所知,这与JS库中包含的代码完全相同。 So I'm not sure why this would work only if extracted from the source and manually put in the file .. 因此,我不确定为什么只有从源代码中提取并手动将其放入文件中才能起作用。

      })
      .each(function() {
        // Señor Hackovitz for rendering HTML elements..
        $(this).data("autocomplete")._renderItem = function(ul, item) {
          return $( "<li></li>" )
              .data( "item.autocomplete", item )
              .append( "<a>" + item.label + "</a>" )
              .appendTo( ul );
        };
      })

I used an .each statement following @Mu Is Too Short's example in case there are many autocompletes available. 如果有许多自动完成功能,我在@Mu Is Too Short的示例之后使用了.each语句。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM