核取方塊條件

<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才會開啟

阅读 2.5k
1 个回答
<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>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题