简体   繁体   中英

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. The checkbox chkReportModuleName is an array. When it is clicked i want only the checkboxes in that row to be checked.

The jquery which i have tried is as follows:

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

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

<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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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