简体   繁体   English

禁用表的同一列中的所有复选框,而无需选中一个jQuery

[英]disable all the checkbox in the same column of a table without the checked one jquery

Hello I have a dynamic generated table where i have several columns with checkboxes .what i want when i check one check box, all the checkboxes in the same column will be disabled but not this one ,and again if i uncheck it all checkbox will enable again vice versa and this action will be same for every check box in that specific column. 您好,我有一个动态生成的表,其中有几列带有复选框。当我选中一个复选框时,我想要什么,同一列中的所有复选框都将被禁用,但不会禁用该复选框;如果我取消选中所有复选框,则将再次启用所有复选框再次相反,对于该特定列中的每个复选框,此操作将相同。

 for(var i=1;i<=10;i++) { var row = "<tr class='active' style='font-size=10px;height:3px;'>" + "<td style='height:3px;' class='text-center'><input type='text' name='txtQuantity' class='form-control' /></td>" + "<td style='height:3px;' class='text-center'><input type='text' name='txtQuantity' class='form-control' /></td>" + "<td style='height:3px;' class='text-center'><input type='checkbox' value=''></td>" + "<td style='height:3px;' class='text-center'><input type='checkbox' value=''></td>" + "<td style='height:3px;' class='text-center'><input type='checkbox' id='sam' value=''></td>" + "</tr>"; $('#tblComponent tbody').append(row); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <table class="table table-responsive-lg header-fixed table-striped table-bordered table-condensed table-hover dataTable no-footer " id="tblComponent"> <thead class="" style="background-color: #B7472A; color: white;font-size: 13px; "> <tr> <th class="text-center">Id</th> <th class="text-center">Name</th> <th class="text-center">pass</th> <th class="text-center">fail</th> <th class="text-center">sample</th> </tr> </thead> <tbody style=""></tbody> </table> 

done only for normal checkboxs but not for those in table columns 仅对普通复选框执行,而对表列中的复选框则不执行

the table code is like below 表格代码如下

<table class="table table-responsive-lg header-fixed table-striped table-bordered table-condensed table-hover dataTable no-footer " id="tblComponent">
                            <thead class="" style="background-color: #B7472A; color: white;font-size: 13px; ">
                                <tr>
                                    <th class="text-center">Id</th>
                                    <th class="text-center">Name</th>
                                    <th class="text-center">pass</th>
                                    <th class="text-center">fail</th>
                                     <th class="text-center">sample</th>


                                </tr>
                            </thead>
                            <tbody style=""></tbody>
                        </table>

and disable checkbox code is ,but its not helping 和禁用复选框代码是,但没有帮助

 $(document).on("click","input[type=checkbox]",function(){
 if($(this).is(':checked')){
 $('input[type=checkbox]').attr('disabled',true);
  $(this).attr('disabled','');
  }
  else{
  $('input[type=checkbox]').attr('disabled','');
  }
 });

I want this to be done for Sample column checkboxes only.I added the table generation snippet Thanks for the help. 我只想对Sample column复选框进行此操作。我添加了表格生成代码段感谢您的帮助。

Assign a class to all the checkboxes in the column sample and on the click event, disable all the checkboxes with that class except the current one. 将一个类分配给该列示例中的所有复选框,并在click事件上,禁用除当前类之外的所有与该类对应的复选框。 .not(this) . .not(this)

 for(var i=1;i<=10;i++) { var row = "<tr class='active' style='font-size=10px;height:3px;'>" + "<td style='height:3px;' class='text-center'><input type='text' name='txtQuantity' class='form-control' /></td>" + "<td style='height:3px;' class='text-center'><input type='text' name='txtQuantity' class='form-control' /></td>" + "<td style='height:3px;' class='text-center'><input type='checkbox' value=''></td>" + "<td style='height:3px;' class='text-center'><input type='checkbox' value=''></td>" + "<td style='height:3px;' class='text-center'><input type='checkbox' class='sample' value=''></td>" + "</tr>"; $('#tblComponent tbody').append(row); } $(document).on("click","input[class=sample]",function(){ if($(this).is(':checked')){ $('input[class=sample]').not(this).attr('disabled',true); } else{ $('input[class=sample]').attr('disabled',false); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <table class="table table-responsive-lg header-fixed table-striped table-bordered table-condensed table-hover dataTable no-footer " id="tblComponent"> <thead class="" style="background-color: #B7472A; color: white;font-size: 13px; "> <tr> <th class="text-center">Id</th> <th class="text-center">Name</th> <th class="text-center">pass</th> <th class="text-center">fail</th> <th class="text-center">sample</th> </tr> </thead> <tbody style=""></tbody> </table> 

UPDATE: Added new code according to your comment. 更新:根据您的注释添加了新代码。 Here we disable all the checkboxes in siblings of current tr . 在这里,我们禁用当前tr同级中的所有复选框。

 for(var i=1;i<=10;i++) { var row = "<tr class='active' style='font-size=10px;height:3px;'>" + "<td style='height:3px;' class='text-center'><input type='text' name='txtQuantity' class='form-control' /></td>" + "<td style='height:3px;' class='text-center'><input type='text' name='txtQuantity' class='form-control' /></td>" + "<td style='height:3px;' class='text-center'><input type='checkbox' value=''></td>" + "<td style='height:3px;' class='text-center'><input type='checkbox' value=''></td>" + "<td style='height:3px;' class='text-center'><input type='checkbox' class='sample' value=''></td>" + "</tr>"; $('#tblComponent tbody').append(row); } $(document).on("click","input[class=sample]",function(){ if($(this).is(':checked')) { $(this).closest('tr').siblings().find($('input[type=checkbox]')).attr('disabled',true); } else{ $('input[type=checkbox]').attr('disabled',false); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <table class="table table-responsive-lg header-fixed table-striped table-bordered table-condensed table-hover dataTable no-footer " id="tblComponent"> <thead class="" style="background-color: #B7472A; color: white;font-size: 13px; "> <tr> <th class="text-center">Id</th> <th class="text-center">Name</th> <th class="text-center">pass</th> <th class="text-center">fail</th> <th class="text-center">sample</th> </tr> </thead> <tbody style=""></tbody> </table> 

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

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