This question already has answers here:
Check boxes clicked all
(2个答案)
5年前关闭。
在表单中,我有一个字段集,其中有几个复选框。系列中的最后一个复选框将标记为“无”,选中时将取消选中所有其他选项。再次单击其中一个选项时,应取消选中该点的“无”框。我真的很纠结于怎么做。这是我的html
</fieldset>
<fieldset class="checkboxx">
<legend>What Extreme Sports Do You You Play?(check all that apply):</legend>

    <label><input type="checkbox" id="chkSkateboarding" name="chkSkateboarding"
                  <?php if($Skateboarding) echo ' checked="checked" ';?>
                  value="Skateboarding" tabindex="250"> Skateboarding</label>

<label><input type="checkbox" id="chkPaintball" name="chkPaintball"
                  <?php if($Paintball) echo ' checked="checked" ';?>
                  value="Paintball" tabindex="260"> Paintball</label>

    <label><input type="checkbox" id="chkSnowboarding" name="chkSnowboarding"
                  <?php if($Paintball) echo ' checked="checked" ';?>
                  value="Snowboarding" tabindex="270"> Snowboarding</label>

    <label><input type="checkbox" id="chkMountainBiking" name="chkMountainBiking"
                  <?php if($MountainBiking) echo ' checked="checked" ';?>
                  value="MountainBiking" tabindex="280"> Mountain Biking</label>

    <label><input type="checkbox" id="chkWakeboarding" name="chkWakeboarding"
                  <?php if($Wakeboarding) echo ' checked="checked" ';?>
                  value="Wakeboarding" tabindex="285"> Wakeboarding</label>

    <label><input type="checkbox" id="chkRockClimbing" name="chkRockClimbing"
                  <?php if($RockClimbing) echo ' checked="checked" ';?>
                  value="RockClimbing" tabindex="290"> Rock Climbing</label>

    <label><input type="checkbox" id="chkNone" name="chkNone"
                  <?php if($None) echo ' checked="checked" ';?>
                  value="None" tabindex="291"> None</label>

</fieldset>

最佳答案

技术应简单如下:
1-当选中任何其他复选框时,我们应该取消选中(无)复选框。

var allOtherCheckboxes = $("input:checkbox:not(#chkNone)");

    // uncheck the (none) checkbox when any other checkbox is selected
    allOtherCheckboxes.change(function(){
    $("#chkNone").prop('checked', false);
    });

2-对于(无)复选框,我们应取消选中所有其他复选框。
$("#chkNone").change(function(){
        var noneStatus = $(this).prop("checked");
        if(noneStatus === true)
        {
            // remove the checked state from other checkboxes in the page
            allOtherCheckboxes.each(function(el){
                //alert("found");
                $(this).prop('checked', false);
            });
        }
    });

我已经为您创建了这个小提琴,它包含了您正在寻找的:http://jsfiddle.net/hakeero/p9bCD/2/

09-10 05:31
查看更多