jQuery如果复选框被选中diasble其他复选框 [英] jQuery if checkbox is checked diasble other checkbox
本文介绍了jQuery如果复选框被选中diasble其他复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有HTML代码:
< input type =checkboxname =allvalue =all > ALL
< input type =checkboxname =agentvalue =agent> Agent
< input type =checkboxname =companyvalue =company >公司
< input type =checkboxname =branchvalue =branch>分支
如果我选中全部,其他复选框将被禁用,如果我取消选中,其他复选框
$( input [name ='all'])。change(function(){
if(this.checked){
$(input [name ='agent' disabled,true);
}
});
解决方案
您的代码只会禁用。您应该使用 checked
值作为已停用的属性,根据检查/取消选中状态切换状态:
var subinputs = $(input [name = agent],input [name = company],input [name = branch]); $(input [name ='all'] ){subinputs.prop(disabled,this.checked);});
< script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js>< / script>< input type = checkboxname =allvalue =all> ALL< input type =checkboxname =agentvalue =agent> Agent< input type =checkboxname = value =company>公司< input type =checkboxname =branchvalue =branch>分支
>
I have HTML code:
<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
If I check all, other checkbox becomes disabled, if I uncheck, other checkbox is back enabled.
I have tried doing this, using the Script below.
$("input[name='all']").change(function() {
if(this.checked) {
$("input[name='agent']").prop("disabled",true);
}
});
解决方案
Your code will only do the part of disabling. You should rather use checked
value as disabled property to toggle state based on check/uncheck status:
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
这篇关于jQuery如果复选框被选中diasble其他复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文