简体   繁体   English

使用jQuery选中特定的Tbody中的所有复选框

[英]Select all check boxes in specific tbody using jquery

I have a table that gets dynamically populated. 我有一个可以动态填充的表。

The main table will have 3 <tbody> sections in it and rows in each one. 主表中将包含3个<tbody>节,每个表中将包含一行。

Each row will have a checkbox in it. 每行中都有一个复选框。

What I want to do is use a jquery selector that matches the <tbody> id. 我想做的是使用与<tbody> id匹配的jquery选择器。 The id is also dynamically created so it is not a constant value but uses a variable. ID也是动态创建的,因此它不是常量值,而是使用变量。

The jquery I am trying to use is this: 我试图使用的jQuery是这样的:

$(":input[id$='_ErrorCheckbox']").click(function () {
        var wbId = $(this).attr('id').split("_")[0].toString().trim();

        $('tbody#' + wbId + ' _ErrorVisits tr td input[type="checkbox"]').prop('checked', $(this).prop('checked'));
    });

I also tried using this statement to grab the rows in that <tbody> but it did not work (threw an unknown syntax error): 我还尝试使用此语句来获取该<tbody>的行,但是它不起作用(引发了未知的语法错误):

$('tbody[id$=' + wbId + ' _ErrorVisits] tr td input[type="checkbox"]').prop('checked', $(this).prop('checked'));

The first method above, does not throw any errors but does not select anything. 上面的第一种方法不会引发任何错误,但不会选择任何内容。

What is the correct way to use jquery selectors to grab a specific <tbody> element using its id? 使用jquery选择器通过其ID捕获特定<tbody>元素的正确方法是什么?

Edit: Here is the html table output: 编辑:这是html表输出:

<table id="workbookTable_25116" cellpadding="10">
                    <thead>
                        <tr>
                            <th><input type="checkbox" id="25116 _SelectAllCheckbox"></th><th>Visit Timepoint</th><th>Visit Information</th><th>Notifications/Errors</th><th>Preview SQC Analysis</th>
                        </tr>
                    </thead>
                    <tbody id="25116 _ErrorVisits">
                        <tr><td></td><td></td><td></td><td></td></tr>
                        <tr><td><input type="checkbox" id="25116 _ErrorCheckbox"></td><td><b>Visits in Error</b></td><td></td><td></td></tr><tr style="background-color: red;"><td><input type="checkbox" id="9be44178-7273-4057-9607-acb135114b39 _25116 _Checkbox" name="visit_cb"></td><td>Week 8</td><td>1008-001, Week 8, 14005_MRI_Femur v1.0</td><td>This visit has a task in Error status!</td><td><input type="button" id="9be44178-7273-4057-9607-acb135114b39 _25116 _ResetTaskButton" value="Reset Task"></td></tr><tr style="background-color: red;"><td><input type="checkbox" id="367e7f6a-ec3a-476f-a1e3-b3eecf18db8b _25116 _Checkbox" name="visit_cb"></td><td>Week 16</td><td>1008-001, Week 16, 14005_MRI_Femur v1.0</td><td>This visit has a task in Error status!</td><td><input type="button" id="367e7f6a-ec3a-476f-a1e3-b3eecf18db8b _25116 _ResetTaskButton" value="Reset Task"></td></tr>
                    </tbody>
                    <tbody id="25116 _NotPreviouslySQCvisits">
                        <tr><td></td><td></td><td></td><td></td></tr>
                        <tr><td><input type="checkbox" id="25116 _NotSQCCheckbox"></td><td><b>First Time SQC Visits</b></td><td></td><td></td></tr><tr style="background-color: yellow;"><td><input type="checkbox" id="d38bc66a-561e-4a12-9d95-4979eb3e9b9b _25116 _Checkbox" name="visit_cb"></td><td>Week 4</td><td>1008-001, Week 4, 14005_MRI_Femur v1.0</td><td></td><td><input type="button" id="d38bc66a-561e-4a12-9d95-4979eb3e9b9b _25116 _PreviewButton" value="SQC Analysis Preview"></td></tr>
                    </tbody>
                    <tbody id="25116 _PreSQCvisits">
                        <tr><td></td><td></td><td></td><td></td></tr>
                        <tr><td><input type="checkbox" id="25116 _PreSQCCheckbox"></td><td><b>Previously SQC Visits</b></td><td></td><td></td></tr><tr style="background-color: greenyellow;"><td><input type="checkbox" id="25d62e5f-b69d-4f87-aaaa-09c44e06f1cb _25116 _Checkbox" name="visit_cb"></td><td>Week 12</td><td>1008-001, Week 12, 14005_MRI_Femur v1.0</td><td></td><td><input type="button" id="25d62e5f-b69d-4f87-aaaa-09c44e06f1cb _25116 _PreviewButton" value="SQC Analysis Preview"></td></tr>
                    </tbody>
                </table>

Edit: Solution found, needed quotes around id. 编辑:找到解决方案,需要在id周围加上引号。 See my answer below for code. 请参阅下面的代码我的答案。 Thanks for all the help everyone. 谢谢各位的帮助。

The follow line should be changed: 以下行应更改:

$('tbody[id$=' + wbId + ' _ErrorVisits] tr td input[type="checkbox"]').prop('checked', $(this).prop('checked'));

To: 至:

$('tbody[id$=' + wbId + '_ErrorVisits] tr td input[type="checkbox"]').prop('checked', $(this).prop('checked'));

THAT IS : replace ' _ErrorVisits] with '_ErrorVisits] . 那就是 :更换' _ErrorVisits]'_ErrorVisits]

NOTE: I would not advise on the use of id's with spaces but the spaces can be escaped as follows as a quick fix : 注意:我不建议将id's与空格一起使用,但是可以通过以下方法将空格转义,以快速解决问题

Replace wbId with wbId.replace(/ /g, '\\\\\\\\ ') wbId.replace(/ /g, '\\\\\\\\ ')替换wbId

The ideal solution is to fix the ids to remove spaces. 理想的解决方案是修复ID以删除空格。

Sorry everyone, but it turns out I was missing quotes around the id selector. 对不起,每个人,但事实证明我在ID选择器周围缺少引号。

Here is the final jquery code: 这是最终的jQuery代码:

$("tbody[id$='" + wbId + " _ErrorVisits'] tr td input[type='checkbox']").prop('checked', $(this).prop('checked'));

Quotes needed to be placed around the id to prevent a syntax error and because the id had spaces in it. 需要在ID周围加上引号,以防止语法错误以及ID内包含空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM