jquery自动完成突出显示
如何使我的jquery自动完成突出显示用户在任何自动完成的结果中输入的内容? 我使用的代码是:
$("#keyword").autocomplete({ source: "ajax/autocomplete.php?action=keyword", minLength: 2 });
尝试通过链接tomasz发布实现:
$("#keyword").autocomplete({ source: "ajax/autocomplete.php?action=keyword", highlight: function(value, term) { return value.replace(new RegExp("("+term+")", "gi"),'$1'); }, minLength: 2 });
也没有运气。 jQuery autocomplete似乎讨厌我。
更新:感谢David Murdoch,我现在有了答案! 请参阅@ Herman下面答案的副本。
感谢David Murdoch在http://www.vervestudios.co/提供了这个有用的答案:
$.ui.autocomplete.prototype._renderItem = function( ul, item){ var term = this.term.split(' ').join('|'); var re = new RegExp("(" + term + ")", "gi") ; var t = item.label.replace(re,"$1"); return $( "" ) .data( "item.autocomplete", item ) .append( "" + t + "" ) .appendTo( ul ); }; $("#keyword").autocomplete({ source: "ajax/autocomplete.php?action=keyword", minLength: 2 });
认为somone可能会觉得这很有用。 它是一个完整的HTML文档,它使用了上述原则。 我用它作为我的一些工作的原型。
AutoComplete Box with jQuery
希望它能帮助我,因为它帮助了我。