javascript - 选择每个动态表的所有复选框

标签 javascript jquery

我有不止一张 table 。表和行是从数据库生成的。顶部的每个表我都有一个复选框,如果我只单击需要选中的特定表复选框。明智的做法是我得到了更多的表格,并且所有检查都应该独立于每个表格工作。在这里我放了 2 个表的 html。

我的表结构如下:

<table width="800" border="1" cellpadding="2" cellspacing="0" class="inlineTable" style="border-collapse: collapse" bordercolor="#000000">
<tr><td colspan="5" align="center" width="800"><strong>Haweye International&nbsp;(USD)</strong></td></tr>
<tr>
                <th>Sl No</th>
                <th width=130>Tender Description</th>
                <th>Main</th>
                <th><input type="checkbox" id="selecctall"/></th>
                <th title="Selected to Quotation">S2Q</th>
                </tr>
        <tr align='center'><td>1</td><td width='140'>Silicon Free Tape </td><td></td><td><div class='list-item'><input class='checkbox1' type='checkbox' name='member[1]' value='53'></div></td><td title='Selected to Quotation' class="dark"><input type="checkbox" disabled="disabled" name="comparitive[]" value="53"  checked='checked' /></td>
</tr><tr align='center'><td>2</td><td width='140'>UV Sensitive Tape</td><td></td><td><div class='list-item'><input class='checkbox1' type='checkbox' name='member[2]' value='54'></div></td><td title='Selected to Quotation' class="dark"><input type="checkbox" disabled="disabled" name="comparitive[]" value="54"  checked='checked' /></td>
</tr></table>

<table width="800" border="1" cellpadding="2" cellspacing="0" class="inlineTable" style="border-collapse: collapse" bordercolor="#000000">
<tr><td colspan="6" align="center" width="800"><strong>Trend Zone limited&nbsp;(USD)</strong></td></tr>
<tr>
                <th>Sl No</th>
                <th width=130>Tender Description</th>
                <th>Tech %</th>
                <th>Main</th>
                <th><input type="checkbox" id="selecctall"/></th>
                <th title="Selected to Quotation">S2Q</th>
                </tr>
        <tr align='center'><td>1</td><td width='140'>Silicon Free Tape </td><td></td><td></td><td><div class='list-item'><input class='checkbox1' type='checkbox' name='member[3]' value='63'></div></td><td title='Selected to Quotation' class="dark"><input type="checkbox" disabled="disabled" name="comparitive[]" value="63" /></td>
</tr><tr align='center'><td>2</td><td width='140'>UV Sensitive Tape</td><td></td><td></td><td><div class='list-item'><input class='checkbox1' type='checkbox' name='member[4]' value='64'></div></td><td title='Selected to Quotation' class="dark"><input type="checkbox" disabled="disabled" name="comparitive[]" value="64" /></td>
</tr></table>

jquery 如下:

$('.selectAll').click(function(e){
  var tr= $(e.target).closest('tr');
  $('td input:checkbox',tr).prop('checked',this.checked);
});

最佳答案

为每个主复选框提供复选框类名 class="selectAll",JS 将如下所示:

// just in case
// use event delegation here to ensure appended elements
// were bound to this handler
// you can replace `document` with the placeholder
// that dynamic elements inserted with
$(document).on('click','.selectAll', function(e){
  var table= $( this ).closest('table');
  table.find(':checkbox').not( this ).prop('checked',this.checked);
});

DEMO

关于javascript - 选择每个动态表的所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35987810/

相关文章:

javascript - Istanbul 尔覆盖问题 "else path not taken"但显然是

javascript - Window.open with 'noopener' 打开一个新窗口而不是新标签

javascript - 检查数组中的任何值是否与 dom 中的元素匹配,然后将键发送到 Protractor 中的该元素

javascript - 选择其他按钮时,JQuery Mobile 单选按钮保持选中状态。使用 .html 函数

php - 或者像我的 SQL 查询(使用 CodeIgniter 生成)一样,无法使用 jQuery DataTables

javascript - JQuery 添加 <body> 类,需要更多帮助

jQuery css ('padding' ) - Firefox 问题

javascript - Ember 获取路线当前正在转换为

javascript - 在 Jquery 中,如何将部分段落文本包装在 span 中

java - 处理从 Ajax 在 Java 文件中发送的 FormData