javascript - 核取方塊條件
習慣沉默
習慣沉默 2017-05-19 10:47:09
[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才會開啟

習慣沉默
習慣沉默

全部回复(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>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号