如何在下拉菜单中更改时取消选中属于同一类的所有复选框
How to uncheck all checkboxes belonging to the same class on change in a drop down?
我有一个函数来处理下拉列表中的变化:
$('#dropDownChoice').change(function () {
debugger;
var choice= $('#dropDownChoice option:selected').text();
//code to display elements: the divisions that contain the checkboxes
});
$(".chkBox").change(function () {
debugger;
if (this.checked) {
$("." + this.className).not(this).prop("disabled", true);
$(this).closest('div').find(".save").show();
$(this).closest('div').find(':input[type="number"]').prop('disabled', false);
}
else {
//opposite of that in if - block
}
});
复选框(6个)声明为:
<input type="checkbox" class="chkBox" >
现在,当我选中一个复选框时,所有相关的字段都会显示出来,而其他的则被禁用。
我的问题是,当下拉改变,即另一个选项被选中时,我仍然得到之前选择的复选框以及相关字段启用,所有其他复选框不可见。
我试着使用:
$('.chkBox').show();
$('.chkBox').removeAttr('checked');
还有一个自定义函数,我从$('#dropDownChoice').change(function (){}
内部调用但是这行不通。
function ResetChkBox() {
$('.chkBox').prop('checked', false);
$(this).closest('div').find(".save").hide();
}
编辑:HTML: <div id= "main">
<div id="div1" style="padding-left: 20px;">
<input type="checkbox" class="chkBox" >
<input type="number" id="len1" disabled = "disabled"/>
<a href='#' id="btn-save" class="glyphicon glyphicon-floppy-save save" style="display: none;" ></a>
</div>
//other divs each with numeric input fields and a checkbox, all checkboxes of class `chkBox`
</div>
问题说明:
我需要在它上面的下拉框发生变化时显示这些复选框,我已经实现了这一点。
。选择一个复选框,在字段中输入一个值,然后单击保存,复选框就会消失。这件事也完成了。
现在,您从下拉列表中选择其他内容,复选框重新出现,并且必须未选中。这就是我的问题所在。如果您选择了第二个复选框并保存了该值,那么当下拉框更改时,我仍然会选中第二个复选框,并隐藏所有其他复选框,并禁用其对应的字段!
$(".chkBox").change(function () {
debugger;
if (this.checked) {
$(".chkBox").prop("checked", false);
$(".chkBox").prop("disabled", true);
$(this).prop("disabled",false);
$(this).prop("checked",true);
$(this).closest('div').find(".save").show();
$(this).closest('div').find(':input[type="number"]').prop('disabled', false);
}
else {
//opposite of that in if - block
}
});
我不确定这是否是你需要的。
$(".chkBox").change(function () {
$("." + this.className).not(this).prop("disabled", $(this).is(':checked'));
$(this).closest('div').find(':input[type="number"]').prop('disabled', !$(this).is(':checked'));
$(".save").hide();
$(this).closest('div').find(".save").toggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id= "main">
<div id="div1" style="padding-left: 20px;">
<input type="checkbox" class="chkBox" >
<input type="number" id="len1" disabled = "disabled"/>
<a href='#' id="btn-save" class="glyphicon glyphicon-floppy-save save" style="display: none;" >div1</a>
</div>
<div id="div2" style="padding-left: 20px;">
<input type="checkbox" class="chkBox" >
<input type="number" id="len2" disabled = "disabled"/>
<a href='#' id="btn-save2" class="glyphicon glyphicon-floppy-save save" style="display: none;" >div2</a>
</div>
<div id="div3" style="padding-left: 20px;">
<input type="checkbox" class="chkBox" >
<input type="number" id="len3" disabled = "disabled"/>
<a href='#' id="btn-save3" class="glyphicon glyphicon-floppy-save save" style="display: none;" >div3</a>
</div>
</div>
相关文章:
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 正在验证8个真/假复选框或复选框中的2个
- 为复选框javascript指定两个值
- 如何在单击复选框后调用控制器方法
- 从复选框和Selects-KnockoutJS中获取值的总和
- 选中多个具有相同名称的复选框
- 使用Jquery选择或取消选择ListView中的所有复选框
- Jquery表单验证插件-如果选中复选框,如何在提交时执行某些操作
- 使用$.ajax发布多个复选框
- AngularJS单选筛选不适用于Name、Description和Field4复选框值
- 使用jquery选中/取消选中单个复选框
- Angular JS Filter-通过3个复选框进行筛选
- 如果选中了多个复选框,如何添加事件
- Javascript复选框函数:;缺少:在属性id之后"
- JavaScript-切换“;全部检查”;复选框true/false
- 如何从javascript/jquery中的复选框中获取布尔值
- 在Ajax中捕获复选框值
- 显示隐藏复选框
- 如何使用jquery获取同一类的多个复选框的值
- 如何在下拉菜单中更改时取消选中属于同一类的所有复选框