I'm trying to select multiple checkboxes inside a div, if a specific checkbox is selected. Here's what I'm trying to do http://jsfiddle.net/8PLH6/
如果选择一个特定的复选框,我将尝试在一个div中选择多个复选框。下面是我要做的:http://jsfiddle.net/8plh6/。
I'm trying to make this work. If I check "All" all the below checkboxes should be selected. For some reason I can't get the selectors right. Code:
我正在努力做这个工作。如果我选中“All”,则应该选中下面的复选框。由于某种原因,我不能把选择器选对。代码:
<div style="float:left;padding-right:15px;">
<div align="left" style="padding-bottom: 4px; padding-left: 6px;" class="bottom">
<input type="checkbox" value="1" id="all"/><a href="#" title="All">All</a>
</div>
<div align="left" style="padding-bottom: 4px; padding-left: 6px;" class="bottom">
<input type="checkbox" value="1" id="cat417"/><a href="" title="1">1</a>
</div>
<div align="left" style="padding-bottom: 4px; padding-left: 6px;" class="bottom">
<input type="checkbox" value="1" id="cat410"/><a href="" title="2">2</a>
</div>
<div align="left" style="padding-bottom: 4px; padding-left: 6px;" class="bottom">
<input type="checkbox" value="1" id="cat401"/><a href="" title="3">3</a>
</div>
<div align="left" style="padding-bottom: 4px; padding-left: 6px;" class="bottom">
<input type="checkbox" value="1" id="cat415"/><a href="" title="4">4</a>
</div>
</div>
and
和
$(document).ready(function () {
$('#all').change(function () {
$a = $(this).parent('div').parent('div').children('div').children('input[type="checkbox"]');
$b = $(this).attr('checked');
if ($b) {
$($a).attr('checked', true);
} else {
$($a).attr('checked', false);
}
});
});
});
1 个解决方案
#1
2
This can be very tricky. Please use the .prop()
function.
这可能非常棘手。请使用.prop()函数。
$(document).ready(function()
{
$('#all').on("click",function () {
$("input[type='checkbox']").not("#all").each(function()
{
$(this).prop("checked",$("#all").is(":checked"));
});
});
});
jsFiddle: http://jsfiddle.net/8PLH6/3/
jsFiddle:http://jsfiddle.net/8PLH6/3/
#1
2
This can be very tricky. Please use the .prop()
function.
这可能非常棘手。请使用.prop()函数。
$(document).ready(function()
{
$('#all').on("click",function () {
$("input[type='checkbox']").not("#all").each(function()
{
$(this).prop("checked",$("#all").is(":checked"));
});
});
});
jsFiddle: http://jsfiddle.net/8PLH6/3/
jsFiddle:http://jsfiddle.net/8PLH6/3/