简体   繁体   English

选中一个复选框会选中数组中的所有复选框

[英]checking one checkbox checks all the checkbox in an array

I want to check all the other checkboxes if one checkbox is checked.如果选中一个复选框,我想选中所有其他复选框。 My checkbox is an array.我的复选框是一个数组。 If one checkbox is checked i want all the checkbox in that row to be checked.如果选中一个复选框,我希望选中该行中的所有复选框。 My code我的代码

<tr>
  <td class="small">
    <asp:CheckBox ID="chkReportModuleName" runat="server" name="report"></asp:CheckBox>
  </td>
  <td class="particulrs"></td>
  <td class="small">
    <asp:CheckBox ID="chkReportAdd" runat="server" name="reports"></asp:CheckBox>
  </td>
  <td class="small">
    <asp:CheckBox ID="chkReportEdit" runat="server" name="reports"></asp:CheckBox>
  </td>
  <td class="small">
    <asp:CheckBox ID="chkReportDelete" runat="server" name="reports"></asp:CheckBox>
  </td>
  <td class="small">
    <asp:CheckBox ID="chkReportView" runat="server" name="reports"></asp:CheckBox>
  </td>
  <td class="small">
    <asp:CheckBox ID="chkReportPrint" runat="server" name="reports"></asp:CheckBox>
  </td>
  <td class="othr">
    <span><input type="checkbox" id="chkReportActivity1" name="reports" /></span>
  </td>
</tr>

On check of the checkbox named report.I want to check all the checkboxes with the name reports.选中名为 report 的复选框。我想选中名称为 reports 的所有复选框。 The checkbox chkReportModuleName is an array.复选框 chkReportModuleName 是一个数组。 When it is clicked i want only the checkboxes in that row to be checked.单击它时,我只想选中该行中的复选框。

The jquery which i have tried is as follows:我试过的jquery如下:

 $('input[name="ctl00$MainContent$chkTransModuleName"]').click(function () {
   $('input[name="trans"][i]').attr("checked", this.checked);
   $('input[id="MainContent_chkTransAdd"]').attr("checked", this.checked);
   $('input[id="MainContent_chkTransEdit"][i]').attr("checked", this.checked);
   $('input[id="MainContent_chkTransDelete"][i]').attr("checked", this.checked);
   $('input[id="MainContent_chkTransView"][i]').attr("checked", this.checked);
   $('input[id="MainContent_chkTransPrint"][i]').attr("checked", this.checked);
 });

It runs but i want the only those checkboxes to be checked which are in that row.它运行但我希望只选中该行中的那些复选框。

Can someone please help me with this?有人可以帮我吗? Thanks谢谢

assuming the name ctl00$MainContent$chkTransModuleName is correct假设名称ctl00$MainContent$chkTransModuleName是正确的

$('input[name="ctl00$MainContent$chkTransModuleName"]').click(function () {
    var chk = !!this.checked;
    $(this).closest('tr').find('.small > input[type="checkbox"]').attr('checked', chk);    
});

First of all, give the checkboxes CssClass to make them easier to identify:首先,给复选框CssClass使其更容易识别:

<tr>
    <td class="small"><asp:CheckBox ID="chkReportModuleName" CssClass="reportModule" runat="server" name="report"></asp:CheckBox></td>
    <td class="particulrs"></td>
    <td class="small"><asp:CheckBox ID="chkReportAdd" CssClass="checkbox" runat="server" name="reports"></asp:CheckBox></td>
    <td class="small"><asp:CheckBox ID="chkReportEdit" CssClass="checkbox" runat="server" name="reports"></asp:CheckBox></td>
    <td class="small"><asp:CheckBox ID="chkReportDelete" CssClass="checkbox" runat="server" name="reports"></asp:CheckBox></td>
    <td class="small"><asp:CheckBox ID="chkReportView" CssClass="checkbox" runat="server" name="reports"></asp:CheckBox></td>
    <td class="small"><asp:CheckBox ID="chkReportPrint" CssClass="checkbox" runat="server" name="reports"></asp:CheckBox></td>
    <td class="othr">
        <span><input type="checkbox" id="chkReportActivity1" name="reports" /></span>
    </td>
</tr>

Then you can use this jQuerycode:然后你可以使用这个 jQuery 代码:

$('.reportModule').click(function () {
    var $el = $(this), $parentTR = $el.closest("tr"), checked = $el.is(":checked");
    $(".checkbox", $parentTR).attr("checked", checked);
});

This code uses the closest tr element as a context in which to look for the related checkboxes to toggle.此代码使用最近的tr元素作为上下文,在其中查找要切换的相关复选框。

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

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