1

我需要通过复选框的文本而不是值来使用 jQuery 搜索复选框列表项。

下面的代码按值搜索

    $(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>

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

非常感谢

詹姆士

4

1 回答 1

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();
            }
        });
    });
});
于 2020-03-15T14:39:25.990 回答