我正在使用以下jquery来实现php页面中的所有检查效果:-
<script>
$(function(){
$("#checkall").click(function(){
if($("#checkall").attr('checked')){
$(".department").attr('checked', true);
}else{
$(".department").attr('checked', false);
}
});
});
</script>
<input type="checkbox" id="checkall" name="check_all" />
<input type="checkbox" name="department[]" class="department" value="" checked = "checked"/>
并且全选和取消选中所有效果工作正常。但是,它不适用于表单中的重置按钮。我该如何修改 jquery?谢谢!
$("#checkall").click(function(){
$(".department").prop('checked', $(this).is(':checked') );
});
jQuery .prop()
文档有一个部分描述了为什么应该使用它而不是操作属性。
使用 .prop
代替.attr
,请参阅工作演示。
$(function(){
$("#checkall").click(function(){
if($(this).prop('checked')){
$(".department").prop('checked', true);
}else{
$(".department").prop('checked', false);
}
});
});
你的代码甚至可能很短:
$(function(){
$("#checkall").click(function(){
$(".department").prop('checked', $(this).prop('checked'));
});
});
试试这个演示: http://jsfiddle.net/JCWaT/
我的旧回复在这里:复选框选中所有选项
希望符合需求:)
或满足您的需要 http://jsfiddle.net/kJCcP/
法典
$('input[name="Company"],input[name="Country"]').click(function(){
$(this).nextAll('input[name="'+this.name+'Sub"]').prop('checked',this.checked);
});
或
$('#checkall').click(function() {
$(this).nextAll('.department').prop('checked', this.checked);
});