jQuery 按值、名称或 ID 过滤输入?值优选
我有一个 jQuery 脚本,它允许我在文本框中输入内容时过滤列表项...但是,它只过滤标签之外的文本...
有人可以调整这个脚本,以便它过滤值输入,特别是按钮?
在我的 html 文件的头部:
<script type="text/javascript" src="js/jquery.liveFilter.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(ul#filter_me').liveFilter('slide');
});
</script>
这是文本框:
<input type="text" class="filter" name="liveFilter" />
这是列表:
<ul id="filter_me">
<li><input type="button" value="John Slater" /></li>
<li><input type="button" value="Matt Bold" /></li>
<li><input type="button" value="Bob Cool" /></li>
</ul>
如果我输入... John,我会看到以 john John Slater 作为值的按钮。
<input type="button" value="John Slater" />
这是外部 JQuery 文件:
(function (a) {
a.fn.liveFilter = function (d) {
var c = a(this);
var g;
if (a(this).is("ul")) {
g = "li"
} else {
if (a(this).is("ol")) {
g = "li"
} else {
if (a(this).is("table")) {
g = "tbody tr"
}
}
}
var e;
var b;
var f;
a("input.filter").keyup(function () {
f = a(this).val();
e = a(c).find(g + ':not(:Contains("' + f + '"))');
b = a(c).find(g + ':Contains("' + f + '")');
if (d == "basic") {
e.hide();
b.show()
} else {
if (d == "slide") {
e.slideUp(500);
b.slideDown(500)
} else {
if (d == "fade") {
e.fadeOut(400);
b.fadeIn(400)
}
}
}
});
jQuery.expr[":"].Contains = function (j, k, h) {
return jQuery(j).text().toLowerCase().indexOf(h[3].toLowerCase()) >= 0
}
}
})(jQuery);
任何帮助将不胜感激。
谢谢 :)
I have a jQuery script which allows me to filter list items as I type into the textbox....however, it only filters the text out side of the the tags...
Could someone please adapt this script so that it filters the values of inputs, specifically buttons?
In the head of my html file:
<script type="text/javascript" src="js/jquery.liveFilter.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(ul#filter_me').liveFilter('slide');
});
</script>
Here is the text box:
<input type="text" class="filter" name="liveFilter" />
Here is the list:
<ul id="filter_me">
<li><input type="button" value="John Slater" /></li>
<li><input type="button" value="Matt Bold" /></li>
<li><input type="button" value="Bob Cool" /></li>
</ul>
if i typed... John, i would see the button with john John Slater as the value.
<input type="button" value="John Slater" />
Here is the external JQuery file:
(function (a) {
a.fn.liveFilter = function (d) {
var c = a(this);
var g;
if (a(this).is("ul")) {
g = "li"
} else {
if (a(this).is("ol")) {
g = "li"
} else {
if (a(this).is("table")) {
g = "tbody tr"
}
}
}
var e;
var b;
var f;
a("input.filter").keyup(function () {
f = a(this).val();
e = a(c).find(g + ':not(:Contains("' + f + '"))');
b = a(c).find(g + ':Contains("' + f + '")');
if (d == "basic") {
e.hide();
b.show()
} else {
if (d == "slide") {
e.slideUp(500);
b.slideDown(500)
} else {
if (d == "fade") {
e.fadeOut(400);
b.fadeIn(400)
}
}
}
});
jQuery.expr[":"].Contains = function (j, k, h) {
return jQuery(j).text().toLowerCase().indexOf(h[3].toLowerCase()) >= 0
}
}
})(jQuery);
Any help would be greatly appreciated.
Thankyou :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须调整
:Contains
选择器。像这样的事情:我已经评论了这段代码以进行解释。
你可以在这个 fiddle 上测试它。
注意:这绝对不是优化的,它可能无法完全适用于所有类型的元素(除了 input[button]),但它给了你想法。
You have to tweak the
:Contains
selector. Something like this:I've commented the piece of code for explanations.
You can test it on this fiddle.
Note: this is absolutely not optmized and it might not completely work with all types of elements (other than input[button]), but it gives you the idea.