我正在尝试使用jQuery show / hide来控制div的显示,具体取决于是否选中了复选框。 (我也尝试过使用display none / block的CSS方法。)
我可以使用代码来检查复选框然后让div显示或隐藏,但是,我无法检查所有"检查所有"按钮也可以起作用,如果选中了特定复选框,则相应地显示/隐藏div。
我把它放到JSFiddle中:https://jsfiddle.net/gamehendgeVA/2jszhdzw/
有人可以帮我把单选中的复选框显示/隐藏与"检查全部"根据要检查的特定复选框显示和隐藏div的按钮?
感谢您的帮助!
HTML
<div class="panel panel-info">
<div class="panel-heading"><h3 class="panel-title">Search These Domains</h3></div>
<div class="panel-body">
<input type="button" class="btn btn-primary btn-xs" data-toggle="button" value="all domains" id="checkAllDomains" />
<br />
<div class="col-md-6">
<div class="checkbox"><label><input type="checkbox" class="searchDomainCheckbox" id="domainCheckbox1">One</label></div>
<div class="checkbox"><label><input type="checkbox" class="searchDomainCheckbox">Two</label></div>
<div class="checkbox"><label><input type="checkbox" class="searchDomainCheckbox">Three</label></div>
</div>
<div class="col-md-6">
<div class="checkbox"><label><input type="checkbox" class="searchDomainCheckbox">Four</label></div>
<div class="checkbox"><label><input type="checkbox" class="searchDomainCheckbox">Five</label></div>
<div class="checkbox"><label><input type="checkbox" class="searchDomainCheckbox">Six</label></div>
</div>
</div> <!-- /.panel-body -->
</div> <!-- /.panel -->
<div class="panel panel-info" id="div1">
<div class="panel-heading"><h3 class="panel-title">Search These Divisions</h3></div>
<div class="panel-body">
THIS DIV SHOULD ONLY APPEAR WHEN "#domainCheckbox1" is checked, either when checked on its own, or checked when the "all domains" button checks it.
</div> <!-- /.panel-body -->
</div> <!-- /.panel -->
JS
$('#div1').hide();
$('#domainCheckbox1').change(function(){
if($(this).prop("checked")) {
$('#div1').show();
} else {
$('#div1').hide();
}
});
/////////
$(function() {
function updateCheckAllButtonDomains() {
if ($('.searchDomainCheckbox:checked').length == $('.searchDomainCheckbox').length) {
$('#checkAllDomains').val('uncheck all').data('check', false);
} else {
$('#checkAllDomains').val('all domains').data('check', true);
}
}
$('.searchDomainCheckbox').change(function() {
updateCheckAllButtonDomains();
});
$('#checkAllDomains').click(function() {
$('.searchDomainCheckbox').prop('checked', $(this).data('check'));
updateCheckAllButtonDomains();
});
updateCheckAllButtonDomains();
});
答案 0 :(得分:2)
只需触发Change事件就可以了
function updateCheckAllButtonDomains()
{
if ($('.searchDomainCheckbox:checked').length == $('.searchDomainCheckbox').length)
$('#checkAllDomains').val('uncheck all').data('check', false);
else
$('#checkAllDomains').val('all domains').data('check', true);
$("input.searchDomainCheckbox").trigger("change");
}
答案 1 :(得分:1)
这是因为searchDomainCheckbox事件已经附加到元素上。因此,您必须手动触发,如下所示:$('.searchDomainCheckbox').trigger('change');
将此代码放在#checkAllDomains的事件点击上并快乐!
这是我的解决方案:
$('#checkAllDomains').click(function() {
$('.searchDomainCheckbox').prop('checked', $(this).data('check'));
$('.searchDomainCheckbox').trigger('change');
updateCheckAllButtonDomains();
});
注意:将触发器置于updateCheckAllButtonDomains()内部会导致&#34;超出最大调用堆栈大小&#34; 。
答案 2 :(得分:0)
以下是jsFiddle
HTML
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Search These Domains</h3>
</div>
<div class="panel-body">
<input type="checkbox" id="checkAllDomains" />
<label>Check All</label>
<br />
<div class="col-md-6">
<div class="checkbox">
<label>
<input type="checkbox" class="searchDomainCheckbox">One</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="searchDomainCheckbox">Two</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="searchDomainCheckbox">Three</label>
</div>
</div>
<div class="col-md-6">
<div class="checkbox">
<label>
<input type="checkbox" class="searchDomainCheckbox">Four</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="searchDomainCheckbox">Five</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="searchDomainCheckbox">Six</label>
</div>
</div>
</div>
<!-- /.panel-body -->
JAVASCRIPT
$('#checkAllDomains').click(function (event) {
if (this.checked) {
$('.searchDomainCheckbox').each(function () {
this.checked = true;
});
} else {
$('.searchDomainCheckbox').each(function () {
this.checked = false;
});
}
});