jQuery 按值、名称或 ID 过滤输入?值优选

发布于 2024-12-21 21:18:31 字数 2158 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷你仙 2024-12-28 21:18:31

您必须调整 :Contains 选择器。像这样的事情:

jQuery.expr[":"].Contains = function (j, k, h) {

    // get children that have a "value" attribute
    // (added sort-of handling for <select> elements)
    var m = jQuery(j).children('[value], select > option[value]');

    return m.length

        // children were found, base the check on the value
        ? m.filter(function() {
                return this.value &&
                       this.value.toLowerCase().indexOf(h[3].toLowerCase()) >= 0;
            }).length > 0

        // otherwise, base on the "innerText"
        : jQuery(j).text().toLowerCase().indexOf(h[3].toLowerCase()) >= 0;
}

我已经评论了这段代码以进行解释。

你可以在这个 fiddle 上测试它。


注意:这绝对不是优化的,它可能无法完全适用于所有类型的元素(除了 input[button]),但它给了你想法。

You have to tweak the :Contains selector. Something like this:

jQuery.expr[":"].Contains = function (j, k, h) {

    // get children that have a "value" attribute
    // (added sort-of handling for <select> elements)
    var m = jQuery(j).children('[value], select > option[value]');

    return m.length

        // children were found, base the check on the value
        ? m.filter(function() {
                return this.value &&
                       this.value.toLowerCase().indexOf(h[3].toLowerCase()) >= 0;
            }).length > 0

        // otherwise, base on the "innerText"
        : jQuery(j).text().toLowerCase().indexOf(h[3].toLowerCase()) >= 0;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文