【发布时间】:2014-03-12 09:29:34
【问题描述】:
我必须控制另一个复选框的复选框列表的选中状态。
HTML:
<input id="readall" name="readall" type="checkbox" value="1">
<div id="permGrid">
<input id="recipe.read" name="recipe.read" type="checkbox" value="1" rel="read">
<input id="group.read" name="group.read" type="checkbox" value="1" rel="read">
<input id="ingredients.read" name="ingredients.read" type="checkbox" value="1" rel="read">
</div>
JS:
$('#readall').click(function()
{
var checkStatus = $(this).is(':checked');
var checkboxList = $('#permGrid input[rel="read"]');
$(checkboxList).attr('rel', 'read').each(function(index)
{
if(checkStatus == true)
{
$(this).attr('checked', 'checked');
console.log($(this).attr('checked'));
}
else
{
$(this).removeAttr('checked').reload();
console.log($(this).attr('checked'));
}
});
});
上面的代码看起来不错,但检查/取消检查仅在第一次工作。但是当我第二次单击主复选框时,它不会将其他复选框的状态更改为“已选中”。我有什么需要做的吗?
我发现了类似的东西here。我比较了代码和我的代码,这段代码有点相似,但我的代码不起作用。
【问题讨论】:
标签: javascript jquery html checkbox checkboxlist