核取方塊條件

发布于 2022-09-04 19:08:45 字数 1059 浏览 11 评论 0

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弃爱 2022-09-11 19:08:45
<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文