单击单选按钮时选中所有复选框
Select all checkboxes on click of radio button
我已经计算出如何在单击单选按钮时选中页面上的复选框,但这会在复选框的ID上进行选择。我的页面上的复选框是动态生成的,末尾有一个id号。
<input type="checkbox" certificate="false" id="gridcb0" siteid="23445" value="72278" class="custom selector">
Option One<br />
<input type="checkbox" certificate="false" id="gridcb1" siteid="23445" value="72278" class="custom selector">
Option Two<br />
<input type="checkbox" certificate="false" id="gridcb2" siteid="23445" value="72278" class="custom selector">
Option Three<br />
<input type="checkbox" certificate="false" id="gridcb3" siteid="23445" value="72278" class="custom selector">
Option Four<br />
<input type="checkbox" certificate="false" id="gridcb4" siteid="23445" value="72278" class="custom selector">
Option Five<br />
<input type="radio" id="op5" value="1" name="options2" class="custom">
Select All
正如您所看到的,ID是gridcb0
到gridcb5
,但末尾的数字可能有数百。
这里是我的JS
jQuery('#op5').click(function(){
jQuery('#gridcb0').attr('checked', true);
});
目前,这只会选择顶部的复选框,如何更改此JS,以便我循环通过并选中所有必需的复选框?
您可以尝试选择类型如下的元素:
jQuery('#op5').click(function(){
jQuery('input[type="checkbox"]').prop('checked', true);
});
有用:.prp()
它们都共享一个公共类,因此您可以通过它进行选择,并消除在它们之间循环的需要:
jQuery('#op5').click(function(){
jQuery('.custom.selector').attr('checked', true);
});
为此,您甚至可以添加另一个更具语义的类作为选择器。
使用此jQuery代码:
jQuery('#op5').click(function(){
$(".custom").attr("checked","checked");
});
您可以执行以下操作。
jQuery('#op5').click(function(){
jQuery('input[type="checkbox"]').prop('checked', true);});
以下代码运行良好。
<script src="~/Scripts/jquery-1.8.2.js"></script>
<input type="checkbox" certificate="false" id="gridcb0" siteid="23445" value="72278" class="custom-selector"> Option One
<br />
<input type="checkbox" certificate="false" id="gridcb1" siteid="23445" value="72278" class="custom-selector"> Option Two
<br />
<input type="checkbox" certificate="false" id="gridcb2" siteid="23445" value="72278" class="custom-selector"> Option Three
<br />
<input type="checkbox" certificate="false" id="gridcb3" siteid="23445" value="72278" class="custom-selector"> Option Four
<br />
<input type="checkbox" certificate="false" id="gridcb4" siteid="23445" value="72278" class="custom-selector"> Option Five
<br />
<input type="radio" id="op5" value="1" name="options2" class="custom">Select All
<script>
$(document).ready(function () {
$(".custom").change(function () {
$(".custom").parent().find(".custom-selector").prop("checked", true);
})
});
</script>
我刚把你的复选框类改成
自定义选择器
您可以尝试
jQuery('#op5').click(function(){
jQuery('input[type="checkbox"]').attr('checked', true);
});
为了获得更多的控制权,您可以将复选框包装在一个单独的div中。
您需要使用一个应用于所有复选框的类,或者您可以按元素定位它们,例如:
jQuery('#op5').click(function(){
jQuery('input[type=checkbox]').attr('checked', true);
});
JSFIDDLEhttp://jsfiddle.net/a_incarnati/ckr7r0fm/1/
如果您将代码更改为
jQuery('#op5').click(function(){
jQuery(':checkbox').attr('checked', true);
});
它应该起作用。
jQuery(':checkbox')
只是的简写
jQuery('input[type=checkbox]')
jQuery('#op5').click(function(){
jQuery('.selector').prop('checked', true);
});
jQuery('.selector').change(function(){
jQuery('#op5').prop('checked',
jQuery('.selector:checked').length == jQuery('.selector').length);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="checkbox" certificate="false" id="gridcb0" siteid="23445" value="72278" class="custom selector"> Option One
<br />
<input type="checkbox" certificate="false" id="gridcb1" siteid="23445" value="72278" class="custom selector"> Option Two
<br />
<input type="checkbox" certificate="false" id="gridcb2" siteid="23445" value="72278" class="custom selector"> Option Three
<br />
<input type="checkbox" certificate="false" id="gridcb3" siteid="23445" value="72278" class="custom selector"> Option Four
<br />
<input type="checkbox" certificate="false" id="gridcb4" siteid="23445" value="72278" class="custom selector"> Option Five
<br />
<input type="radio" id="op5" value="1" name="options2" class="custom">Select All
相关文章:
- 复选框/单选按钮-添加所选项目的总价
- 将文本框/单选按钮/和下拉列表中的信息添加到dataTable
- 如何防止jQuery中复选框单击时执行多个代码
- 如果用户未选中复选框或选择按钮,如何为复选框或单选按钮设置一些默认值
- 如何在更改节点 JS 表中的复选框时禁用按钮
- 如何使用 rails3-jquery-autocomplete 更新复选框/单选按钮
- IE issue-使用复选框/单选按钮时,jQuery.change()不会触发回调
- 引导复选框/单选按钮不改变<输入>价值
- 在复选框单击中,更改可以单击的按钮
- 样式复选框&单选按钮
- 启用两个提交按钮上的复选框单击
- jQuery缓存刷新复选框单选按钮状态
- 基于复选框单选按钮调整总成本标价
- 自定义复选框/单选按钮,不用ID和FOR,也不用angular.js
- 单击复选框以禁用按钮
- 表单提交的复选框:点击按钮时显示错误
- 我如何有一个复选框/单选按钮张贴到一个文本区,当我点击提交按钮
- 如果我们在鼠标下移时设置preventDefault,然后双击文档,那么它就会阻止复选框/单选标签的默认行为
- Ipad复选框/单选按钮操作缓慢
- 如何在 JavaScript 中创建带有 3 个复选框/单选按钮作为选项的提示