Javascript 单击单选按钮时选中所有复选框
我已经解决了如何单击单选按钮如何选中我页面上的复选框,但是这会选择复选框的ID。我的页面上的复选框是动态生成的,末尾有一个id号Javascript 单击单选按钮时选中所有复选框,javascript,jquery,html,checkbox,Javascript,Jquery,Html,Checkbox,我已经解决了如何单击单选按钮如何选中我页面上的复选框,但是这会选择复选框的ID。我的页面上的复选框是动态生成的,末尾有一个id号 <input type="checkbox" certificate="false" id="gridcb0" siteid="23445" value="72278" class="custom selector"> Option One<br /> <input type="checkbox" certificate="false
<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
目前,这将只选择顶部复选框,如何更改此JS,以便我循环并选中所有必需的复选框?它们都共享一个公共类,因此您可以通过该选项进行选择,并取消循环的需要:
jQuery('#op5').click(function(){
jQuery('.custom.selector').attr('checked', true);
});
您甚至可以添加另一个语义更丰富的类来用作此目的的选择器。它们都共享一个公共类,因此您可以通过该类进行选择,而无需在它们之间循环:
jQuery('#op5').click(function(){
jQuery('.custom.selector').attr('checked', true);
});
您甚至可以添加另一个语义更丰富的类来用作选择器。您可以通过选择具有以下代码类型的元素进行尝试:
jQuery('#op5').click(function(){
jQuery('input[type="checkbox"]').prop('checked', true);
});
帮助:您可以通过选择具有以下代码类型的元素进行尝试:
jQuery('#op5').click(function(){
jQuery('input[type="checkbox"]').prop('checked', true);
});
帮助:您可以试试
jQuery('#op5').click(function(){
jQuery('input[type="checkbox"]').attr('checked', true);
});
要获得更多控制权,您可以将复选框包装在单独的div中。您可以尝试
jQuery('#op5').click(function(){
jQuery('input[type="checkbox"]').attr('checked', true);
});
要获得更多控制权,您可以将复选框包装在一个单独的div中。您需要使用一个应用于所有复选框的类,或者可以按元素将它们作为目标,如:
jQuery('#op5').click(function(){
jQuery('input[type=checkbox]').attr('checked', true);
});
jsFIDLE您需要使用一个应用于所有复选框的类,或者可以按元素将其作为目标,如:
jQuery('#op5').click(function(){
jQuery('input[type=checkbox]').attr('checked', true);
});
jshiddle
jQuery'op5'。单击函数{
jQuery'.selector'.prop'checked',true;
};
jQuery'.selector'.changefunction{
jQuery'op5'。属性'checked',
jQuery'.selector:选中'.length==jQuery'.selector'.length;
};
选择一
选择二
选择三
选择四
选择五
选择全部
jQuery'op5'。单击函数{
jQuery'.selector'.prop'checked',true;
};
jQuery'.selector'.changefunction{
jQuery'op5'。属性'checked',
jQuery'.selector:选中'.length==jQuery'.selector'.length;
};
选择一
选择二
选择三
选择四
选择五
如果将代码更改为,请选择所有
jQuery('#op5').click(function(){
jQuery(':checkbox').attr('checked', true);
});
它应该会起作用
jQuery(':checkbox')
这只是
jQuery('input[type=checkbox]')
如果您将代码更改为
jQuery('#op5').click(function(){
jQuery(':checkbox').attr('checked', true);
});
它应该会起作用
jQuery(':checkbox')
这只是
jQuery('input[type=checkbox]')
使用以下jQuery代码:
jQuery('#op5').click(function(){
$(".custom").attr("checked","checked");
});
使用以下jQuery代码:
jQuery('#op5').click(function(){
$(".custom").attr("checked","checked");
});
你可以像下面这样做
jQuery('#op5').click(function(){
jQuery('input[type="checkbox"]').prop('checked', true);});
你可以像下面这样做
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>
我刚把你的复选框改成了
自定义选择器
下面的代码工作正常
<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>
我刚把你的复选框改成了
自定义选择器
复选框和单选按钮之间的关系是什么?复选框和单选按钮之间的关系是什么?