如何使一个简单的 jQuery 过滤器列表只接受整个单词,因此它不匹配部分单词,只匹配整个单词
http://jsfiddle.net/nicktheandroid/U8T8p/4/
(function($) {
$('.filterinput').keyup(function() {
var filter = $(this).val();
if (filter.length > 2) {
// this finds all links in the list that contain the input,
// and hide the ones not containing the input while showing the ones that do
$(list).find("a:not(:Contains(" + filter + "))").parent().slideUp();
$(list).find("a:Contains(" + filter + ")").parent().slideDown();
} else {
$(list).find("li").slideDown();
}
return false;
})
}(jQuery));
这将基于过滤列表如果您输入的内容与列表中单词的任何部分匹配,则打开。我试图让它只匹配整个单词,所以在输入完整单词之前它不会说它有匹配项。正则表达式会是最好的吗?或者?我需要帮助,我尝试过的方法都没有效果。
http://jsfiddle.net/nicktheandroid/U8T8p/4/
(function($) {
$('.filterinput').keyup(function() {
var filter = $(this).val();
if (filter.length > 2) {
// this finds all links in the list that contain the input,
// and hide the ones not containing the input while showing the ones that do
$(list).find("a:not(:Contains(" + filter + "))").parent().slideUp();
$(list).find("a:Contains(" + filter + ")").parent().slideDown();
} else {
$(list).find("li").slideDown();
}
return false;
})
}(jQuery));
This will filter the list based on if what you've typed matches ANY part of a word in the list. I'm trying to make it only match whole words, so it wont say it has a match until the full word is typed. Would Regex be best? Or? I need help, nothing I've tried has worked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用已经提到的
filter
方法。或者,您可以使用自己的自定义过滤器轻松扩展 jQuery,这是重用代码的好方法。现在像使用任何其他内置过滤器一样使用它
:containsWord('Some word')
。请参阅http://jsfiddle.net/U8T8p/9/
You can make use of the
filter
method as already mentioned. Or you can easily extend jQuery with your own custom filter which is great way to reuse code.and now use it like you would do with any other built-in filter
:containsWord('Some word')
.See http://jsfiddle.net/U8T8p/9/
尝试这样的事情: http://jsfiddle.net/U8T8p/5/
Try something like this: http://jsfiddle.net/U8T8p/5/
您可以使用
.filter
检查文本并将这些元素向下滑动,并将这些元素作为 jQuery 对象传递给.not
以使其他元素向上滑动:< a href="http://jsfiddle.net/U8T8p/8/" rel="nofollow">http://jsfiddle.net/U8T8p/8/。You can use
.filter
to check the texts and slide these elements down, and pass these elements as a jQuery object to.not
to get the other elements to slide up: http://jsfiddle.net/U8T8p/8/.像这样的东西: http://jsfiddle.net/U8T8p/10/
正则表达式限制匹配那些当前输入的文本从单词边界开始。因此,输入“Stat”后,您仍然会匹配“United States”。您可以通过更改为
RegExp('\\b' + a + '\\b', i)
使其匹配完整单词。Something like this: http://jsfiddle.net/U8T8p/10/
The regex limits matches to those where the currently entered text starts at a word boundary. So, having typed 'Stat' you'd still match 'United States'. You can make it match a full word by changing to
RegExp('\\b' + a + '\\b', i)
.