html只选择一个组中的一个复选框

use*_*449 115 html checkbox

那么我怎样才能让用户只选择一个复选框呢?

我知道单选按钮是"理想的",但出于我的目的......它不是.

我有一个字段,用户需要选择其中一个或两个选项,但不能同时选择两个.问题是我需要我的用户也能够取消选择他们的选项,这就是单选按钮失败的原因,因为一旦选择了组,就必须选择一个选项.

我将通过php验证信息,但我仍然希望将用户限制为一个答案,如果他们想要给它.

bPr*_*tik 163

这个片段将:

  • 允许像单选按钮一样分组
  • 就像收音机一样
  • 允许全部取消选择

// the selector will match all input controls of type :checkbox
// and attach a click event handler 
$("input:checkbox").on('click', function() {
  // in the handler, 'this' refers to the box clicked on
  var $box = $(this);
  if ($box.is(":checked")) {
    // the name of the box is retrieved using the .attr() method
    // as it is assumed and expected to be immutable
    var group = "input:checkbox[name='" + $box.attr("name") + "']";
    // the checked state of the group/box on the other hand will change
    // and the current value is retrieved using .prop() method
    $(group).prop("checked", false);
    $box.prop("checked", true);
  } else {
    $box.prop("checked", false);
  }
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<div>
  <h3>Fruits</h3>
  <label>
    <input type="checkbox" class="radio" value="1" name="fooby[1][]" />Kiwi</label>
  <label>
    <input type="checkbox" class="radio" value="1" name="fooby[1][]" />Jackfruit</label>
  <label>
    <input type="checkbox" class="radio" value="1" name="fooby[1][]" />Mango</label>
</div>
<div>
  <h3>Animals</h3>
  <label>
    <input type="checkbox" class="radio" value="1" name="fooby[2][]" />Tiger</label>
  <label>
    <input type="checkbox" class="radio" value="1" name="fooby[2][]" />Sloth</label>
  <label>
    <input type="checkbox" class="radio" value="1" name="fooby[2][]" />Cheetah</label>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 正确的用法是使用$(this).is(":checked")"is"来检查if {...} else {...}这里是否正在检查复选框... http ://jsfiddle.net/zGEaa/31/ (3认同)
  • 你错过了无线电选择器:$("input:checkbox.radio") (3认同)
  • 请注意.attr不再有效,请使用.prop("checked")代替更新版本的jQuery (2认同)

bil*_*can 88

您希望绑定change()处理程序,以便在复选框的状态更改时触发事件.然后,只需取消选中除触发处理程序之外的所有复选框:

$('input[type="checkbox"]').on('change', function() {
   $('input[type="checkbox"]').not(this).prop('checked', false);
});
Run Code Online (Sandbox Code Playgroud)

这是一个小提琴


至于分组,如果你的复选框"群组"都是兄弟姐妹:

<div>
    <input type="checkbox" />
    <input type="checkbox" />
    <input type="checkbox" />
</div>  
<div>
    <input type="checkbox" />
    <input type="checkbox" />
    <input type="checkbox" />
</div>   
<div>
    <input type="checkbox" />
    <input type="checkbox" />
    <input type="checkbox" />
</div>
Run Code Online (Sandbox Code Playgroud)

你可以这样做:

$('input[type="checkbox"]').on('change', function() {
   $(this).siblings('input[type="checkbox"]').prop('checked', false);
});
Run Code Online (Sandbox Code Playgroud)

这是另一个小提琴


如果您的复选框按其他属性分组,例如name:

<input type="checkbox" name="group1[]" />
<input type="checkbox" name="group1[]" />
<input type="checkbox" name="group1[]" />

<input type="checkbox" name="group2[]" />
<input type="checkbox" name="group2[]" />
<input type="checkbox" name="group2[]" />

<input type="checkbox" name="group3[]" />
<input type="checkbox" name="group3[]" />
<input type="checkbox" name="group3[]" />
Run Code Online (Sandbox Code Playgroud)

你可以这样做:

$('input[type="checkbox"]').on('change', function() {
    $('input[name="' + this.name + '"]').not(this).prop('checked', false);
});
Run Code Online (Sandbox Code Playgroud)

这是另一个小提琴

  • 加上一个简单 (2认同)

Que*_*tin 26

单选按钮非常理想.您只需要默认选择的第三个"不"选项.

  • 它不是不灵活,它看起来不正确......它可能在表单和此类应用程序上看起来很好,但我们的复选框有不同的用法. (9认同)
  • 如果这样的改变需要你改变"你的整个设计"那么这表明设计首先是太不灵活了. (8认同)
  • 我强烈建议改变设计.*勾选这两个选项中的0或1*不是常见模式,对用户而言不如*3选择这3个选项* (6认同)
  • 为什么我会改变我的整个设计2个复选框? (4认同)

Eve*_*vdw 9

基于纯JS,已经有一些答案,但没有一个像我希望的那样简洁.

这是我的解决方案,基于使用名称标签(与单选按钮一样)和几行javascript.

function onlyOne(checkbox) {
    var checkboxes = document.getElementsByName('check')
    checkboxes.forEach((item) => {
        if (item !== checkbox) item.checked = false
    })
}
Run Code Online (Sandbox Code Playgroud)
<input type="checkbox" name="check" onclick="onlyOne(this)">
<input type="checkbox" name="check" onclick="onlyOne(this)">
<input type="checkbox" name="check" onclick="onlyOne(this)">
<input type="checkbox" name="check" onclick="onlyOne(this)">
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这个是从其他人那里为我做的 =) (2认同)

Sur*_*ams 6

$("#myform input:checkbox").change(function() {
    $("#myform input:checkbox").attr("checked", false);
    $(this).attr("checked", true);
});
Run Code Online (Sandbox Code Playgroud)

这应该适用于表单中的任意数量的复选框.如果您有其他人不属于该组,请将选择器设置为适用的输入.


小智 6

希望这段代码对您有帮助。

$(document).ready(function(){
$('.slectOne').on('change', function() {
   $('.slectOne').not(this).prop('checked', false);
   $('#result').html($(this).data( "id" ));
   if($(this).is(":checked"))
   	$('#result').html($(this).data( "id" ));
   else
   	$('#result').html('Empty...!');
});
});
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

</head>
<body>
<input type="checkbox" class="slectOne" data-id="1 selected"/>
<input type="checkbox" class="slectOne" data-id="2 selected"/>
<input type="checkbox" class="slectOne" data-id="3 selected"/>
<input type="checkbox" class="slectOne" data-id="4 selected"/>
<input type="checkbox" class="slectOne" data-id="5 selected"/>
<input type="checkbox" class="slectOne" data-id="6 selected"/>
<input type="checkbox" class="slectOne" data-id="7 selected"/>
<input type="checkbox" class="slectOne" data-id="8 selected"/>
<input type="checkbox" class="slectOne" data-id="9 selected"/>
<input type="checkbox" class="slectOne" data-id="10 selected"/>
<span id="result"></span>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

工作链接点击这里


小智 5

这是一个我更喜欢的简单HTML和JavaScript解决方案:

// js函数只允许一次检查一个工作日复选框:

function checkOnlyOne(b){

var x = document.getElementsByClassName('daychecks');
var i;

for (i = 0; i < x.length; i++) {
  if(x[i].value != b) x[i].checked = false;
}
}


Day of the week:
<input class="daychecks" onclick="checkOnlyOne(this.value);" type="checkbox" name="reoccur_weekday" value="Monday" />Mon&nbsp;&nbsp;&nbsp;
<input class="daychecks" onclick="checkOnlyOne(this.value);" type="checkbox" name="reoccur_weekday" value="Tuesday" />Tue&nbsp;&nbsp;&nbsp;
<input class="daychecks" onclick="checkOnlyOne(this.value);" type="checkbox" name="reoccur_weekday" value="Wednesday" />Wed&nbsp;&nbsp;&nbsp;
<input class="daychecks" onclick="checkOnlyOne(this.value);" type="checkbox" name="reoccur_weekday" value="Thursday" />Thu&nbsp;&nbsp;&nbsp;
<input class="daychecks" onclick="checkOnlyOne(this.value);" type="checkbox" name="reoccur_weekday" value="Friday" />Fri&nbsp;&nbsp;&nbsp;
<input class="daychecks" onclick="checkOnlyOne(this.value);" type="checkbox" name="reoccur_weekday" value="Saturday" />Sat&nbsp;&nbsp;&nbsp;
<input class="daychecks" onclick="checkOnlyOne(this.value);" type="checkbox" name="reoccur_weekday" value="Sunday" />Sun&nbsp;&nbsp;&nbsp;<br /><br />
Run Code Online (Sandbox Code Playgroud)


ben*_*123 5

我的版本:使用数据属性和 Vanilla JavaScript

<div class="test-checkbox">
    Group One: <label>
        <input type="checkbox" data-limit="only-one-in-a-group" name="groupOne" value="Eat" />Eat</label>
    <label>
        <input type="checkbox" data-limit="only-one-in-a-group" name="groupOne" value="Sleep" />Sleep</label>
    <label>
        <input type="checkbox" data-limit="only-one-in-a-group" name="groupOne" value="Play" />Play</label>
    <br />
    Group Two: <label>
        <input type="checkbox" data-limit="only-one-in-a-group" name="groupTwo" value="Fat" />Fat</label>
    <label>
        <input type="checkbox" data-limit="only-one-in-a-group" name="groupTwo" value="Comfort" />Comfort</label>
    <label>
        <input type="checkbox" data-limit="only-one-in-a-group" name="groupTwo" value="Happy" />Happy</label>
</div>
<script>
    let cbxes = document.querySelectorAll('input[type="checkbox"][data-limit="only-one-in-a-group"]');
    [...cbxes].forEach((cbx) => {
        cbx.addEventListener('change', (e) => {
            if (e.target.checked)
                uncheckOthers(e.target);
        });
    });
    function uncheckOthers (clicked) {
        let name = clicked.getAttribute('name');
        // find others in same group, uncheck them
        [...cbxes].forEach((other) => {
            if (other != clicked && other.getAttribute('name') == name)
                other.checked = false;
        });
    }
</script>
Run Code Online (Sandbox Code Playgroud)