javascript - 两组复选框,如果第一个被选中,第二个不能被选中

标签 javascript html checkbox

我有两个复选框“组”(尽管第一组仅包含一个复选框),但是如果用户选中第一组中的复选框,我想限制检查第二组中复选框的能力...

<div>
  <h3>First group</h3>
  <label>
    <input type="checkbox" value="1" name="group1" />If checked, checks all the checkboxes in 2nd group</label>
  <label>
</div>
<div>
  <h3>Second Group</h3>
  <label>
    <input type="checkbox" class="radio" value="1" name="group2" />1</label>
  <label>
    <input type="checkbox" class="radio" value="1" name="group2" />1</label>
</div>

最佳答案

<div>
  <h3>First group</h3>
  <label>
    <input type="checkbox" value="1" name="group1" id='Grp1'/>If checked, checks all the checkboxes in 2nd group</label>
  <label>
</div>
<div>
  <h3>Second Group</h3>
  <label>
    <input type="checkbox" class="radio Grp2" value="1" name="group2" />1</label>
  <label>
    <input type="checkbox" class="radio Grp2" value="1" name="group2" />1</label>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
    $('#Grp1').change(function() {
        if($(this).is(":checked"))
        { 
            $('.Grp2').prop('disabled', true);
        }
        else
        {
            $('.Grp2').prop('disabled', false);
        }
    });
</script>

关于javascript - 两组复选框,如果第一个被选中,第二个不能被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32564397/

相关文章:

javascript - 加载页面后避免下载

javascript - 将鼠标悬停并单击 li 元素更改类

JavaScript 错误 Uncaught SyntaxError : Unexpected identifier

c# - 如何在 ASP.Net mvc3 Razor 中使用多个复选框

javascript - 复选框选中的属性不适用于 Pug

javascript - 如何使用索引文件查看完整日历?

javascript - Three.js - ply 文件 - 为什么是无色的?

javascript - 表单验证逻辑 Vanilla JS

html - CSS 中的部分边框

javascript - 如何使用 react 钩子(Hook)实现多个复选框