如何取消选中 select 所有复选框,当他们的 child 中的任何一个被删除时 selected
How to uncheck select all checkbox when any one of their child got deselected
如何取消选中 select 所有 checkbox(parent)
,当它们的任何一个 child 复选框被取消selected!
从代码片段中,当我们 select 所有复选框以及当我们从他们的 select 中删除 select 用户时,所有复选框都应该取消选中(因为所有复选框都不是 selected)
$(document).ready(function() {
$('#Admin').click(function() {
var checked = $(this).prop('checked');
$('#tab-10').find('input:checkbox').prop('checked', checked);
});
$('#Sales').click(function() {
var checked = $(this).prop('checked');
$('#tab-20').find('input:checkbox').prop('checked', checked);
});
// i have so may like this in that case how can i do that.?
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#tab-10"> First </a></li>
<li class=""><a data-toggle="tab" href="#tab-20"> Second </a></li>
</ul>
<div class="tab-content vehicle">
<div id="tab-10" class="tab-pane active">
<div class="panel-body">
<div class="row">
<div class="col-md-3">
<div class="checkbox">
<label><input type="checkbox" id="Admin"><b>Select All</b></label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label><input type="checkbox" name="pages[]" value="2">Dealerships</label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label><input type="checkbox" name="pages[]" value="23">Users</label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label><input type="checkbox" name="pages[]" value="59">Third-Party Exports</label>
</div>
</div>
</div>
</div>
</div>
<br><br>
<div id="tab-20" class="tab-pane ">
<div class="panel-body">
<div class="row">
<div class="col-md-3">
<div class="checkbox">
<label><input type="checkbox" id="Sales"/><b>Select All</b></label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label><input type="checkbox" name="Second[]" value="2">Second tab 1</label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label><input type="checkbox" name="Second[]" value="23">Second tab 1</label>
</div>
</div>
</div>
</div>
</div>
</div>
实际上您没有针对您的问题进行任何编码(也就是说,当您取消选中任何复选框而不是 select-all 时,使 select-all 未选中)。
按如下操作:-
$(document).ready(function() {
$('#Admin').click(function() {
var checked = $(this).prop('checked');
$('#tab-10').find('input:checkbox').prop('checked', checked);
});
$('#Sales').click(function() {
var checked = $(this).prop('checked');
$('#tab-20').find('input:checkbox').prop('checked', checked);
});
$('.tab-pane').find('input:checkbox:not(:first)').click(function() {
if (!$(this).is(':checked')) {
$(this).closest('.tab-pane').find('input:checkbox:first').prop('checked', false);
} else {
var checkbox_length = $(this).closest('.tab-pane').find('input:checkbox:not(:first)').length;
var checked_check_box_length = $(this).closest('.tab-pane').find('input:checkbox:not(:first):checked').length;
if (checkbox_length == checked_check_box_length) {
$(this).closest('.tab-pane').find('input:checkbox:first').prop('checked', true);
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#tab-10"> First </a></li>
<li class=""><a data-toggle="tab" href="#tab-20"> Second </a></li>
</ul>
<div class="tab-content vehicle">
<div id="tab-10" class="tab-pane active">
<div class="panel-body">
<div class="row">
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" id="Admin"><b>Select All</b></label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" name="pages[]" value="2">Dealerships</label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" name="pages[]" value="23">Users</label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" name="pages[]" value="59">Third-Party Exports</label>
</div>
</div>
</div>
</div>
</div>
<br>
<br>
<div id="tab-20" class="tab-pane ">
<div class="panel-body">
<div class="row">
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" id="Sales" /><b>Select All</b></label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" name="Second[]" value="2">Second tab 1</label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" name="Second[]" value="23">Second tab 1</label>
</div>
</div>
</div>
</div>
</div>
</div>
工作fiddle:- http://jsfiddle.net/am6rgdw1/
将以下代码添加到您的 js 中:
$('input[name="pages[]"]').click(function(){
$('input[name="pages[]"]').each(function() {
var checked = $(this).attr('checked');
if(!checked){
$('#Admin').attr('checked', false);
return false;
}
});
});
对于动态选项卡 ID,我是这样做的:
这是我的 html 和 php:
<div class="tab-content vehicle">
<?php $active='active'; foreach ( $check_menu as $y) {?>
<div id="tabs-<?=$y?>" class="tab-pane <?=$active?>">
<div class="panel-body">
<div class="row">
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" class="SelectAll_Dynamic" id="<?=$y?>" />
<b>Select All</b>
</label>
</div>
</div>
<?php foreach($menus as $x): if ($x['parent_id'] ==$y):?>
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" name="pages[]" value="<?=$x['id']?>"
<?php if (array_search($x['id'],$my_menu,true))
{
echo "checked ";
}
?>
><?=$x['name']?></label>
</div>
</div>
<?php endif; $active="";
endforeach;?>
</div></div>
</div>
<?php } ?>
</div>
这是我的脚本:
<script>
$(document).ready(function() {
$('.SelectAll_Dynamic').click(function() {
var select_Id = this.id;
var checked = $(this).prop('checked');
$('#tabs-'+select_Id).find('input:checkbox').prop('checked', checked);
alert(select_Id);
});
$('.tab-pane').find('input:checkbox:not(:first)').click(function() {
if (!$(this).is(':checked')) {
$(this).closest('.tab-pane').find('input:checkbox:first').prop('checked', false);
} else {
var checkbox_length = $(this).closest('.tab-pane').find('input:checkbox:not(:first)').length;
var checked_check_box_length = $(this).closest('.tab-pane').find('input:checkbox:not(:first):checked').length;
if (checkbox_length == checked_check_box_length) {
$(this).closest('.tab-pane').find('input:checkbox:first').prop('checked', true);
}
}
});
});
</script>
如何取消选中 select 所有 checkbox(parent)
,当它们的任何一个 child 复选框被取消selected!
从代码片段中,当我们 select 所有复选框以及当我们从他们的 select 中删除 select 用户时,所有复选框都应该取消选中(因为所有复选框都不是 selected)
$(document).ready(function() {
$('#Admin').click(function() {
var checked = $(this).prop('checked');
$('#tab-10').find('input:checkbox').prop('checked', checked);
});
$('#Sales').click(function() {
var checked = $(this).prop('checked');
$('#tab-20').find('input:checkbox').prop('checked', checked);
});
// i have so may like this in that case how can i do that.?
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#tab-10"> First </a></li>
<li class=""><a data-toggle="tab" href="#tab-20"> Second </a></li>
</ul>
<div class="tab-content vehicle">
<div id="tab-10" class="tab-pane active">
<div class="panel-body">
<div class="row">
<div class="col-md-3">
<div class="checkbox">
<label><input type="checkbox" id="Admin"><b>Select All</b></label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label><input type="checkbox" name="pages[]" value="2">Dealerships</label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label><input type="checkbox" name="pages[]" value="23">Users</label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label><input type="checkbox" name="pages[]" value="59">Third-Party Exports</label>
</div>
</div>
</div>
</div>
</div>
<br><br>
<div id="tab-20" class="tab-pane ">
<div class="panel-body">
<div class="row">
<div class="col-md-3">
<div class="checkbox">
<label><input type="checkbox" id="Sales"/><b>Select All</b></label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label><input type="checkbox" name="Second[]" value="2">Second tab 1</label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label><input type="checkbox" name="Second[]" value="23">Second tab 1</label>
</div>
</div>
</div>
</div>
</div>
</div>
实际上您没有针对您的问题进行任何编码(也就是说,当您取消选中任何复选框而不是 select-all 时,使 select-all 未选中)。
按如下操作:-
$(document).ready(function() {
$('#Admin').click(function() {
var checked = $(this).prop('checked');
$('#tab-10').find('input:checkbox').prop('checked', checked);
});
$('#Sales').click(function() {
var checked = $(this).prop('checked');
$('#tab-20').find('input:checkbox').prop('checked', checked);
});
$('.tab-pane').find('input:checkbox:not(:first)').click(function() {
if (!$(this).is(':checked')) {
$(this).closest('.tab-pane').find('input:checkbox:first').prop('checked', false);
} else {
var checkbox_length = $(this).closest('.tab-pane').find('input:checkbox:not(:first)').length;
var checked_check_box_length = $(this).closest('.tab-pane').find('input:checkbox:not(:first):checked').length;
if (checkbox_length == checked_check_box_length) {
$(this).closest('.tab-pane').find('input:checkbox:first').prop('checked', true);
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#tab-10"> First </a></li>
<li class=""><a data-toggle="tab" href="#tab-20"> Second </a></li>
</ul>
<div class="tab-content vehicle">
<div id="tab-10" class="tab-pane active">
<div class="panel-body">
<div class="row">
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" id="Admin"><b>Select All</b></label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" name="pages[]" value="2">Dealerships</label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" name="pages[]" value="23">Users</label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" name="pages[]" value="59">Third-Party Exports</label>
</div>
</div>
</div>
</div>
</div>
<br>
<br>
<div id="tab-20" class="tab-pane ">
<div class="panel-body">
<div class="row">
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" id="Sales" /><b>Select All</b></label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" name="Second[]" value="2">Second tab 1</label>
</div>
</div>
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" name="Second[]" value="23">Second tab 1</label>
</div>
</div>
</div>
</div>
</div>
</div>
工作fiddle:- http://jsfiddle.net/am6rgdw1/
将以下代码添加到您的 js 中:
$('input[name="pages[]"]').click(function(){
$('input[name="pages[]"]').each(function() {
var checked = $(this).attr('checked');
if(!checked){
$('#Admin').attr('checked', false);
return false;
}
});
});
对于动态选项卡 ID,我是这样做的:
这是我的 html 和 php:
<div class="tab-content vehicle">
<?php $active='active'; foreach ( $check_menu as $y) {?>
<div id="tabs-<?=$y?>" class="tab-pane <?=$active?>">
<div class="panel-body">
<div class="row">
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" class="SelectAll_Dynamic" id="<?=$y?>" />
<b>Select All</b>
</label>
</div>
</div>
<?php foreach($menus as $x): if ($x['parent_id'] ==$y):?>
<div class="col-md-3">
<div class="checkbox">
<label>
<input type="checkbox" name="pages[]" value="<?=$x['id']?>"
<?php if (array_search($x['id'],$my_menu,true))
{
echo "checked ";
}
?>
><?=$x['name']?></label>
</div>
</div>
<?php endif; $active="";
endforeach;?>
</div></div>
</div>
<?php } ?>
</div>
这是我的脚本:
<script>
$(document).ready(function() {
$('.SelectAll_Dynamic').click(function() {
var select_Id = this.id;
var checked = $(this).prop('checked');
$('#tabs-'+select_Id).find('input:checkbox').prop('checked', checked);
alert(select_Id);
});
$('.tab-pane').find('input:checkbox:not(:first)').click(function() {
if (!$(this).is(':checked')) {
$(this).closest('.tab-pane').find('input:checkbox:first').prop('checked', false);
} else {
var checkbox_length = $(this).closest('.tab-pane').find('input:checkbox:not(:first)').length;
var checked_check_box_length = $(this).closest('.tab-pane').find('input:checkbox:not(:first):checked').length;
if (checkbox_length == checked_check_box_length) {
$(this).closest('.tab-pane').find('input:checkbox:first').prop('checked', true);
}
}
});
});
</script>