繁体   English   中英

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

[英]Two groups of checkboxes, if first one is checked second can not be

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

<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>

我使用了一个简单的jQuery脚本来解决它。 最初它并没有完全按照我想要的去做,但是我设法用php解决了它,因为我必须确保如果有人选中了第二组中的复选框,则它不会进入数据库。

$('#checkbox1').click(function() {
             if( $(this).is(':checked')) {
                 $("#checkbox2").hide();
             } else {
                 $("#checksbox2").show();
             }
         });

至于php,我只是使用三元运算符来确保如果选中了ID为checkbox1的复选框,那么我想忽略其余的复选框...

$smnrs = !empty($_POST['all']) ? explode(';', $_POST['all']) : $_POST['seminars'];

在这种情况下,$ _POST ['all']指的是第一个复选框,如果单击该复选框,它将隐藏包含其他复选框的div。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM