【问题标题】:Search checkboxlist item with jQuery by TEXT通过 TEXT 使用 jQuery 搜索复选框列表项
【发布时间】:2020-03-15 14:31:17
【问题描述】:

我需要通过 jQuery 的 TEXT 搜索复选框列表项,而不是按值。

下面的代码按值搜索

    $(document).ready(function () {
    $("#maincontent_txtSearchTo").on("keyup", function () {
        debugger;
        var g = $(this).val().toLowerCase();
        $("#maincontent_chkQuestionTo tr td input").each(function () {
            var s = $(this).val().toLowerCase();
            $(this).parent().parent()[s.indexOf(g) !== -1 ? 'show' : 'hide']();
        });
    });
});

我需要它来按TEXT搜索。

请看下面生成的代码示例

    <input name="ctl00$maincontent$txtSearchTo" type="text" id="maincontent_txtSearchTo" Placeholder="Search..." />
    <table id="maincontent_chkQuestionTo">
    <tr>
        <td><input id="maincontent_chkQuestionTo_0" type="checkbox" name="ctl00$maincontent$chkQuestionTo$0" value="2057" />
<label for="maincontent_chkQuestionTo_0">What is money in finance?</label>
</td>
    </tr><tr>
        <td><input id="maincontent_chkQuestionTo_1" type="checkbox" name="ctl00$maincontent$chkQuestionTo$1" value="2058" />
<label for="maincontent_chkQuestionTo_1">What is Good?</label>
</td>
    </tr>
</table>

如果您能提供帮助,不胜感激。

非常感谢

詹姆斯

【问题讨论】:

    标签: jquery asp.net search checkbox checkboxlist


    【解决方案1】:

    我假设文本,你的意思是标签。如果是这样,您需要在标签上使用 .html()。

    试试下面的代码;

    $(document).ready(function () {
        $("#maincontent_txtSearch").on("keyup", function () {
            debugger;
            var g = $(this).val().toLowerCase();
            $("#maincontent_chkQuestion tr td input").each(function () {
                var td = $(this).parent();
                var label = $(td).find("label");
    
                if($(label).html().toLowerCase().indexOf(g) == -1){
                   $(td).parent().hide();
                }else{
                   $(td).parent().show();
                }
            });
        });
    });
    

    【讨论】:

    • 亲爱的杰丁,确实是标签中的文字。该代码正在运行,但在搜索时,它不会搜索第一个字符。能否请您修改。例如,当我搜索 What 时,我没有得到任何结果,但是当我输入 hat 时,我得到了答案
    • @Tairon 我编辑了代码并在 .html() 上添加了 .toLowerCase(),请使用更新后的脚本。
    • 非常感谢杰丁。欢迎提供更多建议。我正在寻找一个学习 JQuery 的好地方/教程。请你给我建议。谢谢
    • @Tairon 我对这些不是很熟悉。我所做的是搜索我需要的东西。例如,我想学习如何从表单中获取所有值,我搜索“jquery 获取所有表单值”或“jquery json 表单值”,然后出现最流行的 Stackoverflow 答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 2018-10-13
    • 1970-01-01
    相关资源
    最近更新 更多