javascript - 核取方塊條件
本文介绍了javascript - 核取方塊條件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
<input id="a1" type="checkbox">
<label for="a1">1</label>
<input id="a2" type="checkbox">
<label for="a2">2</label>
<input id="a3" type="checkbox">
<label for="a3">3</label>
<input id="a4" type="checkbox">
<label for="a4">4</label>
<input id="a5" type="checkbox">
<label for="a5">5</label>
<script>
$(function()
{
disabledOtherBox();
$("#a1").click(disabledOtherBox);
disabledA1Box();
$("#a2,#a3,#a4,#5").click(disabledA1Box);
});
function disabledOtherBox()
{
if (this.checked)
{
$("#a2,#a3,#a4,#a5").attr("disabled", true);
}
else
{
$("#a2,#a3,#a4,#a5").removeAttr("disabled");
}
}
function disabledA1Box()
{
if (this.checked)
{
$("#a1").attr("disabled", true);
}
else
{
$("#a1").removeAttr("disabled");
}
}
</script>
以上是我寫的條件
如果a1勾選
a2~5鎖住
若a2~a5其中一個勾選
則a1鎖住
我想再加一個條件但始終搞不定
就是假設a2~5中有一個是勾選的(或兩個、三個、四個都是有勾選的)
則a1就鎖住
除非a2~5都沒有勾選
則a1才會開啟
解决方案
<script>
$(function(){
var a1 = $("#a1");
var other = $("#a2,#a3,#a4,#a5");
a1.click(function(){
disabledBox(other,this.checked)
});
other.click(function() {
var r = false;
other.each(function() {
if(this.checked) {
r = true;
return false;
}
})
disabledBox(a1,r)
});
//如a1已勾选,则#a2,#a3,#a4,#5锁住
//disabledBox(other,a1.attr('checked'))
//如#a2,#a3,#a4,#5其中一个或多个已勾选,则a1锁住
other.each(function() {
if(this.checked) {
disabledBox(a1,true)
return false;
}
})
});
function disabledBox(el,disabled){
el.attr("disabled", !!disabled);
}
</script>
这篇关于javascript - 核取方塊條件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文