将值更改为动态父和子复选框
Issue changing value into dynamic parent and child checkboxes
这是实时演示更新的
问题:试图根据动态复选框更改值:
当我点击父复选框时,应将子上的值更改为"1"
<input type="checkbox" class="parentCheckBox" value="1">
<input type="checkbox" class="childCheckBox" value="1">
<input type="checkbox" class="childCheckBox" value="1">
当我取消选中父项时,子项上的值应为"0"
<input type="checkbox" class="parentCheckBox" value="0">
<input type="checkbox" class="childCheckBox" value="0">
<input type="checkbox" class="childCheckBox" value="0">
代码:
<script src="https://code.jquery.com/jquery-2.2.1.js"></script>
<div>
<input type="checkbox" class="parentCheckBox" /> Parent 1 <a onclick="document.getElementById('div_1').style.display='';return false;"><img src="https://cdn0.iconfinder.com/data/icons/ie_Bright/512/plus_add_green.png" height="20" width="20"></a> <a onclick="document.getElementById('div_1').style.display='none';return false;"><img src="https://cdn0.iconfinder.com/data/icons/ie_Bright/512/minus_remove_green.png" height="20" width="20"></a><br />
<div id="div_1" style="display:none;">
<ul>
<li><input type="checkbox" class="childCheckBox" />Child 1-1 </li>
<li><input type="checkbox" class="childCheckBox" />Child 1-2 </li>
<li><input type="checkbox" class="childCheckBox" />Child 1-3 </li>
</ul>
</div>
</div>
<div>
<input type="checkbox" class="parentCheckBox" /> Parent 2 <a onclick="document.getElementById('div_2').style.display='';return false;"><img src="https://cdn0.iconfinder.com/data/icons/ie_Bright/512/plus_add_green.png" height="20" width="20"></a> <a onclick="document.getElementById('div_2').style.display='none';return false;"><img src="https://cdn0.iconfinder.com/data/icons/ie_Bright/512/minus_remove_green.png" height="20" width="20"></a><br />
<div id="div_2" style="display:none;">
<ul>
<li><input type="checkbox" class="childCheckBox" />Child 2-1 </li>
<li><input type="checkbox" class="childCheckBox" />Child 2-2 </li>
<li><input type="checkbox" class="childCheckBox" />Child 2-3 </li>
</ul>
</div>
</div>
脚本:
$(document).on('change', '.parentCheckBox', function() {
var that = $(this);
that.closest('div').find('.childCheckBox').prop('checked', that.is(':checked'));
$(this).val($(this).prop('checked')?1:0);
});
$(document).on('change', '.childCheckBox', function() {
var that = $(this);
var par = that.closest('ul');
var c = par.find('.childCheckBox').filter(':checked').length;
var parChk = par.closest('div').parent().find('.parentCheckBox');
var checked = c > 0;
$(this).val($(this).prop('checked')?1:0);
parChk.prop('checked', checked);
console.log(checked);
});
添加更新子值的逻辑。
$(document).on('change', '.parentCheckBox', function() {
var that = $(this);
that.closest('div').find('.childCheckBox').prop('checked', that.is(':checked')).val(that.is(':checked')?1:0);
});
$(document).on('change', '.childCheckBox', function() {
var that = $(this);
$(this).val($(this).prop('checked') ? 1 : 0);
var par = that.closest('ul');
var c = par.find('.childCheckBox').filter(':checked').length;
var parChk = par.closest('div').parent().find('.parentCheckBox');
var checked = c > 0;
parChk.prop('checked', checked);
console.log(checked);
});
我不能100%确定你想要完成什么,但这里有两个我认为你想要做的非常直接的例子。
这通常不是使用复选框的方式。它们有一个值集,默认情况下,所有复选框的值都为"on"。但是您也可以使用1或任何其他值。没有必要改变它们的价值。如果复选框被选中,决定它是否会发布该值,如果这是一个表单。
但是,为了演示如何根据更改来更改值,我举了两个例子。
简化示例:
HTML:
<ul>
<li><input type="checkbox" class="childCheckBox">Child 1-1 </li>
<li><input type="checkbox" class="childCheckBox">Child 1-2 </li>
<li><input type="checkbox" class="childCheckBox">Child 1-3 </li>
</ul>
JavaScript:
// When checkbox is changed
$('.childCheckBox').on('change', function(e){
// Check if its checked
if($(this).prop('checked')) {
// If checked, set value to 1
$(this).val(1);
} else {
// If not checked, set value to 0
$(this).val(0);
}
// Log value of changed checkbox
console.log($(this).val());
});
https://jsbin.com/mewefegopi/edit?html,js,输出
带有父母的较长示例:
HTML:
<div class="wrapper">
<input type="checkbox" class="parentCheckBox" /> Parent 1
<ul>
<li><input type="checkbox" class="childCheckBox">Child 1-1 </li>
<li><input type="checkbox" class="childCheckBox">Child 1-2 </li>
<li><input type="checkbox" class="childCheckBox">Child 1-3 </li>
</ul>
</div>
<div class="wrapper">
<input type="checkbox" class="parentCheckBox" /> Parent 2
<ul>
<li><input type="checkbox" class="childCheckBox">Child 2-1 </li>
<li><input type="checkbox" class="childCheckBox">Child 2-2 </li>
<li><input type="checkbox" class="childCheckBox">Child 2-3 </li>
</ul>
</div>
JavaScript:
// When parent checkbox is changed
$('.parentCheckBox').on('change', function(e){
var self = this;
// Get all child checkboxes
var childs = $(this).closest('.wrapper').find('.childCheckBox');
// Go though all childs
$(childs).each(function(i, e){
// If parent is checked set child value to 1
if($(self).prop('checked')) {
$(this).val(1);
} else { // ...else set child value to 0
$(this).val(0);
}
});
});
https://jsbin.com/qisizudupa/edit?html,js,输出
$(document).on('click', '.childCheckBox', function(e) {
var childCheckBoxInput = $(e.target);
if (childCheckBoxInput.is(':checked')) {
chilCheckBoxInput.val(1);
} else {
childCheckBoxInput.val(0);
}
});
相关文章:
- JavaScript-取决于多个下拉选择或复选框的动态链接
- 动态添加复选框和输入框 jQuery
- 动态创建唯一的复选框
- 基于所选下拉列表值创建动态复选框
- “动态创建的表”上的复选框(每行最多只能选择1个)
- 动态填充复选框
- 动态选中文本区域中现有项目的复选框组
- 函数更改不适用于动态创建的表的复选框
- 在react中切换动态创建的复选框
- 如何根据用户选择动态更改多个复选框的编号
- 如何将jquery数组添加到html表单中,以将复选框动态添加到对话框中
- 将复选框动态添加到 jquery dilaogue 框中
- 聚合物核心标签和核心复选框动态
- HTML 复选框动态附加到列表,但 JavaScript 不起作用
- 如何勾选“复选框”动态- jQuery移动版
- AngularJS复选框动态ng-true-value表达式
- Angular js基于复选框动态绑定数据列表
- 通过选择复选框动态创建弹出表单
- 使用javascript中的复选框动态添加表列
- 使用 Javascript 将复选框动态添加到 Symfony 2 表单中