【问题标题】:How to highlight jQuery Autocomplete results that match the text already typed?如何突出显示与已输入文本匹配的 jQuery 自动完成结果?
【发布时间】:2011-08-22 11:11:15
【问题描述】:

我已经为客户端实现了 jQuery Autocomplete。现在他们希望我突出显示(例如,使粗体)结果中与他们输入的文本匹配的部分。

例如用户输入“某物”,结果如下:

某事一个

某事 b

另一个某事

某事其他

这个功能是否内置在 jQuery 自动完成中?如果有,是什么?

或者这是我必须自己实现的东西?如果是这样,我应该从哪里开始?

【问题讨论】:

标签: javascript jquery jquery-ui jquery-autocomplete jquery-ui-autocomplete


【解决方案1】:

我之前也有同样的要求。

下面的代码可能对你有用。

“你需要小心选择器”

<script>
  $("#input-search-box")
    .autocomplete({
      //your code
    })
    .data("autocomplete")._renderItem = function(ul, item) {
    var items = $("<li></li>").data("ui-autocomplete-item", item);
    //match and key variables highlight the user's typed input rendering in bold blue for dropdown items.
    var match = new RegExp("^" + this.term, "i"); //i makes the regex case insensitive

    //change initial typed characters of all item.label   replace is plain JS
    var key = item.label.replace(
      match,
      '<span class="required-drop">' + this.term + '</span>'
    );

    items.html("<a>" + key + "</a>").append(ul);
  }; 
</script>

如果您需要更多帮助来实现此代码,请告诉我。

【讨论】:

  • 将 'g' 添加到正则表达式标志以突出显示所有匹配项,而不仅仅是一个
猜你喜欢
  • 2011-08-26
  • 1970-01-01
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
  • 2011-04-11
  • 1970-01-01
  • 2013-01-31
相关资源
最近更新 更多