简体   繁体   English

从选中了复选框的表中获取td列的所有值

[英]Get all the values of td columns from table where checkbox is checked

I have a table as below: 我有一张桌子,如下所示:

表结构 .. ..

There have been multiple questions asked for getting the values but in this case I should always have a parent item name. 为了获得值,已经询问了多个问题,但是在这种情况下,我应该始终具有父项名称。 Suppose If a user selected only one subitem in "Shirts", then I should be able to get all the values from the selected tr and with that I need parent item name also ie "shirts" and if some one clicks on all the subitems of a parent item, then all the values of all tr are need to be in some sort of array object on click of a "Save" button. 假设如果用户仅在“衬衫”中选择了一个子项,那么我应该能够从选定的tr中获得所有值,并且我还需要父项名称,即“衬衫”,并且如果有人点击了所有子项一个父项,则单击“保存”按钮后,所有tr的所有值都必须位于某种数组对象中。 I am trying hard to do this. 我正在努力做到这一点。 Any help would be really appreciated. 任何帮助将非常感激。 Though I have attached the HTML but this HTML is being generated at run time. 尽管我已经附上了HTML,但是该HTML正在运行时生成。

HTML: HTML:

<table>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>Name</td>
        <td>Sub Item</td>
        <td>User Input</td>

    </tr>
    <tr>
        <td>
            <input type="checkbox" id="chkGroup1" class="cls1" onclick="checkUncheckAll(this);" />
        </td>
        <td>Shirts
        </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" class="cls1" name="Group1" onclick="CheckCorrespondingHeader(this);" /></td>
        <td>&nbsp;</td>
        <td>Item1</td>
        <td>SubItem1</td>
        <td>
            <input id="1datepicker" name="1datepicker" type="text" /><script>

            </script></td>

    </tr>
    <tr>
        <td>
            <input type="checkbox" class="cls1" name="Group1" onclick="CheckCorrespondingHeader(this);" /></td>
        <td>&nbsp;</td>
        <td>Item2</td>
        <td>SubItem2</td>
        <td>
            <input id="2datepicker" name="2datepicker" type="text" /><script>

            </script></td>

    </tr>
    <tr>
        <td>
            <input type="checkbox" class="cls1" name="Group1" onclick="CheckCorrespondingHeader(this);" /></td>
        <td>&nbsp;</td>
        <td>Item3</td>
        <td>SubItem3</td>
        <td>
            <input id="3datepicker" name="3datepicker" type="text" /><script>

            </script></td>

    </tr>
    <tr>
        <td>
            <input type="checkbox" id="chkGroup2" class="cls2" onclick="checkUncheckAll(this);" />
        </td>
        <td>Jeans
        </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" class="cls2" name="Group2" onclick="CheckCorrespondingHeader(this);" /></td>
        <td>&nbsp;</td>
        <td>Item4</td>
        <td>SubItem4</td>
        <td>
            <input id="4datepicker" name="4datepicker" type="text" /><script>

            </script></td>

    </tr>
    <tr>
        <td>
            <input type="checkbox" class="cls2" name="Group2" onclick="CheckCorrespondingHeader(this);" /></td>
        <td>&nbsp;</td>
        <td>Item5</td>
        <td>SubItem5</td>
        <td>
            <input id="5datepicker" name="5datepicker" type="text" /><script>

            </script></td>

    </tr>
    <tr>
        <td>
            <input type="checkbox" class="cls2" name="Group2" onclick="CheckCorrespondingHeader(this);" /></td>
        <td>&nbsp;</td>
        <td>Item6</td>
        <td>SubItem6</td>
        <td>
            <input id="6datepicker" name="6datepicker" type="text" /><script>

            </script></td>

    </tr>
</table>

Script code looks like below: 脚本代码如下所示:

<script>
    function checkUncheckAll(sender) {
        var chkElements = document.getElementsByClassName(sender.className);
        for (var i = 0; i < chkElements.length; i++) {
            chkElements[i].checked = sender.checked;
        }
    }

    function CheckCorrespondingHeader(sender) {
        ControlLength = $("[name='" + sender.name + "']").length;
        var countchecks = 0;
        $("[name='" + sender.name + "']").each(function () {
            if ($(this).prop('checked') == true) {
                countchecks = countchecks + 1;
            }
        });
        if (ControlLength == countchecks) {
            $("#chk" + sender.name).attr('checked', 'checked');
        }
        else {
            $("#chk" + sender.name).prop('checked', false);
        }
    }

    function PickAllCheckedRows() {

    }
</script>

As far as I can tell your code should work if you fix one issue. 据我所知,如果您解决了一个问题,您的代码应该可以工作。 You are determining the number of sub rows that need to be checked to make the header row be checked using $("[name='" + sender.name + "']").length; 您正在确定使用$("[name='" + sender.name + "']").length;来检查标题行所需的子行数$("[name='" + sender.name + "']").length; . But unless I'm mistaken sender.name is never set. 但是除非我误会,否则sender.name永远不会设置。 Of course if you set it this still won't work because your each function will include the header row. 当然,如果您将其设置为无效,因为每个函数都将包含标题行。 There are several solutions to this but I would recommend using a data attribute instead of the name attribute like so: 有几种解决方案,但我建议使用data属性而不是name属性,如下所示:

Markup: 标记:

<table>
  <tr>
    <!-- head -->
    <td><input type="checkbox" data-head-for-group="Group1" ... /></td>
  </tr>
  <tr>
    <!-- row 1 -->
    <td><input type="checkbox" data-in-group="Group1" ... /></td>
  </tr>
  <tr>
    <!-- row 2 -->
    <td><input type="checkbox" data-in-group="Group1" ... /></td>
  </tr>
  <tr>
    <!-- head -->
    <td><input type="checkbox" data-head-for-group="Group2" ... /></td>
  </tr>
  <tr>
    <!-- row 3 -->
    <td><input type="checkbox" data-in-group="Group2" ... /></td>
  </tr>
</table>

Script: 脚本:

function CheckCorrespondingHeader(sender) {
  var group = $("[data-in-group='" + sender.data('headForGroup') + "']");
  var groupSize = group.length;
  var countchecks = 0;
  group.each(function () {
    if ($(this).prop('checked') === true) {
       countchecks = countchecks + 1;
    }
  });
  if (groupSize === countchecks) {
    $(sender).attr('checked', 'checked');
  } else {
    $(sender).prop('checked', false);
  }
}

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

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