jQuery的,如果复选框被选中diasble其他复选框
我的HTML代码:jQuery的,如果复选框被选中diasble其他复选框
<input type="checkbox" name="all" value="all">ALL
<input type="checkbox" name="agent" value="agent" >Agent
<input type="checkbox" name="company" value="company">Company
<input type="checkbox" name="branch" value="branch">Branch
如果我检查所有,其他复选框被禁用,如果我取消,其他复选框又回来了启用。
我试过这样做,使用下面的脚本。
$("input[name='all']").change(function() {
if(this.checked) {
$("input[name='agent']").prop("disabled",true);
}
});
您的代码只会执行禁用部分。你还是使用checked
价值为禁用属性基于选中/清除状态切换状态:
var subinputs = $("input[name=agent],input[name=company],input[name=branch]");
$("input[name='all']").change(function() {
subinputs.prop("disabled",this.checked);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="all" value="all">ALL
<input type="checkbox" name="agent" value="agent" >Agent
<input type="checkbox" name="company" value="company">Company
<input type="checkbox" name="branch" value="branch">Branch
请检查下面的代码,还有很多其他的方法,但你的HTML是这样的方式哟做到这一点
$("input[name='all']").change(function() {
if(this.checked) {
$(this).siblings('input:checkbox').prop("disabled",true);
}
else {
$("input[type='checkbox']").removeAttr("disabled");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="all" value="all">ALL
<input type="checkbox" name="agent" value="agent" >Agent
<input type="checkbox" name="company" value="company">Company
<input type="checkbox" name="branch" value="branch">Branch
@MilindAnantwar检查我已经写下面,以启用此也尝试演示,你会看到它工作正常 –
$(“input [name ='all'] “).removeAttr(” 无效“);这是删除所有禁用attr行 - –
哦....知道了:) –
添加缺少的东西:
$("input[name='all']").change(function() {
if(this.checked) {
$("input[name='all'] input").prop("disabled",true);
}
else{
$("input[name='all'] input").prop("disabled",false);
}});
绑定的变化处理,那么就取消所有的复选框,除了一个检查:下面
$('input').on('change', function() {
$('input').not(this).prop('checked', false); });
//I have added a id field in your first check box and add a class rest of checkboxes.
//This code is working you can use this
$(function(){
$('#all_employee').on('click',function(){
if($(this).is(':checked') === true){
$('.employee').prop('disabled','disabled');
}else{
$('.employee').prop("disabled", false);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" name="all" value="all" id="all_employee" >ALL
<input type="checkbox" name="agent" value="agent" class="employee" >Agent
<input type="checkbox" name="company" value="company" class="employee">Company
<input type="checkbox" name="branch" value="branch" class="employee">Branch
检查答案 –