为突出多个jQuery.autocomplete关键字 [英] Highlight multiple keywords for jQuery.autocomplete

查看:166
本文介绍了为突出多个jQuery.autocomplete关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery 自动完成插件,但我有一些问题,其结果突出显示。当找到一个匹配,但输入的关键字包含空格,没有突出显示。例如:

I'm using the jQuery Autocomplete plugin, but I'm having some problems with the result highlighting. When a match is found, but the entered keyword contains spaces, there's no highlighting. Example:

搜索=富,结果=富巴,显示=

search = "foo", result = "foo bar", displayed = "foo bar"

搜索=富巴,结果=富巴,显示=富巴

search = "foo ba", result = "foo bar", displayed = "foo bar"

所以,我想这个使用亮点选项中的自动完成功能的修复,你可以使用一个函数做一些定制的东西的结果。目前,我有这样的:

So, I'm trying to fix this using the highlight option of the autocomplete function where you can use a function to do some custom stuff with the results. Currently I have this:

$('.autocomplete').autocomplete('getmatch.php', {
    highlight: function(match, keywords) {
    	keywords = keywords.split(' ').join('|');
    	return match.replace(/(get|keywords|here)/gi,'<b>$1</b>');
    }
});

替换函数替换所有的字符串匹配单词,一个大胆的版本,作品。但是,我不知道怎么弄的关键字进入该功能。不过,我觉得我会分裂他们,然后加入他们'|',所以富巴结束了像富|酒吧。但是,这样的事情似乎并没有工作:

The replace function replaces all the matched words in the string with a bold version, that works. However, I don't know how to get the keywords into that function. I though I'd split them, and then join them with a '|', so "foo bar" ends up like "foo|bar". But something like this doesn't seem to work:

return match.replace(/(keywords)/gi,'<b>$1</b>'); // seen as text, I think

return match.replace('/'+(keywords)+'/gi','<b>$1</b>'); // nothing either

任何想法?

推荐答案

使用的<一个href=\"https://developer.mozilla.org/en/Core%5FJavaScript%5F1.5%5FReference/Global%5FObjects/RegExp\"><$c$c>RegExp构造创建的正则表达式的从一个字符串对象:

Use the RegExp constructor to create a RegExp object from a string:

$('.autocomplete').autocomplete('getmatch.php', {
    highlight: function(match, keywords) {
        keywords = keywords.split(' ').join('|');
        return match.replace(new RegExp("("+keywords+")", "gi"),'<b>$1</b>');
    }
});

这篇关于为突出多个jQuery.autocomplete关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆