如何使一组复选框互斥?

saj*_*jad 17 javascript jquery

我必须制作相互冲动的复选框.我遇到过很多例子,它给出了一个复选框组的例子.一个例子是http://blog.schuager.com/2008/09/mutually-exclusive-checkboxes-with.html.

在我的情况下,我在同一页面上有许多复选框组,所以我希望它像这个例子一样工作.这里有一个asp.net代码隐藏示例,但我想在客户端代码中执行此操作.

我怎样才能在JavaScript中执行此操作?

我决定使用ajax互斥复选框扩展器.到目前为止给出的解决方案基本上是基于单选按钮.这个链接真的帮了我.. http://www.asp.net/ajax/videos/how-do-i-use-the-aspnet-ajax-mutuallyexclusive-checkbox-extender

Rup*_*war 14

当有单选按钮时使用Mutual Checkbox是一个坏主意但仍然可以按如下方式执行此操作

HTML

<div>    
    Red: <input id="chkRed" name="chkRed" type="checkbox" value="red" class="checkbox">
    Blue: <input id="chkBlue" name="chkBlue" type="checkbox" value="blue" class="checkbox">
    Green: <input id="chkGreen" name="chkGreen" type="checkbox" value="green" class="checkbox">
</div>

<div>
    Mango: <input id="chkRed" name="chkMango" type="checkbox" value="Mango" class="checkbox">
    Orange: <input id="chkBlue" name="chkOrange" type="checkbox" value="Orange" class="checkbox">
    Banana: <input id="chkGreen" name="chkBanana" type="checkbox" value="Banana" class="checkbox">
</div>
Run Code Online (Sandbox Code Playgroud)

jQuery的

$('div .checkbox').click(function () { 
                 checkedState = $(this).attr('checked');
                  $(this).parent('div').children('.checkbox:checked').each(function () {
                      $(this).attr('checked', false);
                  });
                  $(this).attr('checked', checkedState);
});
Run Code Online (Sandbox Code Playgroud)

这里是小提琴


And*_*y E 10

就像我在评论中说的那样,你应该真正使用<radio>元素.给他们相同的名字,他们的工作方式几乎相同:

<label><input type="radio" name="option" value="Option 1">Option 1</label>
<label><input type="radio" name="option" value="Option 2">Option 2</label>
Run Code Online (Sandbox Code Playgroud)

唯一显着的区别是,一旦选择其中一个,其中至少有一个必须打开(即,您不能再次取消选中它们).

如果您真的觉得需要使用复选框,请提醒自己禁用JavaScript的用户可以选择所有选项.如果您仍然觉得有必要这样做,那么您需要为每个复选框组分配一个唯一的类名.然后,处理每个checkbox元素的change事件,并取消选中与单击的元素匹配相同类名的所有其他元素.

  • +1.他也可能修改他们在JS中的外观,以便他们看起来像复选框,如果他真的需要(虽然我不确定这是一个好主意),参见 http://stackoverflow.com/questions/279421/can-you-style-an-html-radio-button-to-look-like-a-checkbox.至于取消选中行为,解决方法可能是提供一个默认(空?)值的单选按钮. (2认同)

One*_*und 5

我希望这个会奏效

HTML

A <input type="checkbox" class="alpha" value="A" /> | 
B <input type="checkbox" class="alpha" value="B" /> | 
C <input type="checkbox" class="alpha" value="C" /> 
<br />

1 <input type="checkbox" class="num" value="1" /> |
2 <input type="checkbox" class="num" value="2" /> |
3 <input type="checkbox" class="num" value="3" /> 
Run Code Online (Sandbox Code Playgroud)

JavaScript

// include jQuery library
var enforeMutualExcludedCheckBox = function(group){
    return function() {
      var isChecked= $(this).prop("checked");
      $(group).prop("checked", false);
      $(this).prop("checked", isChecked);
    }
};

$(".alpha").click(enforeMutualExcludedCheckBox(".alpha"));
$(".num").click(enforeMutualExcludedCheckBox(".num"));
Run Code Online (Sandbox Code Playgroud)

好吧,单选按钮应该是在相互排斥的选项中使用的按钮,尽管我遇到过这样一种情况,即客户端更喜欢从零到一个选定的项目,并且 javaScript 的复选框运行良好。

更新

看着我的回答,我意识到两次引用 css 类是多余的。我更新了代码以将其转换为 jquery 插件,并根据个人喜好创建了两种解决方案

获取所有检查相互排除的复选框

$.fn.mutuallyExcludedCheckBoxes = function(){
    var $checkboxes = this; // refers to selected checkboxes

    $checkboxes.click(function() {
      var $this = $(this),
          isChecked = $this.prop("checked");

      $checkboxes.prop("checked", false);
      $this.prop("checked", isChecked);
    });
};

// more elegant, just invoke the plugin
$("[name=alpha]").mutuallyExcludedCheckBoxes();
$("[name=num]").mutuallyExcludedCheckBoxes();
Run Code Online (Sandbox Code Playgroud)

HTML

A <input type="checkbox" name="alpha" value="A" /> | 
B <input type="checkbox" name="alpha" value="B" /> | 
C <input type="checkbox" name="alpha" value="C" /> 
<br />

1 <input type="checkbox" name="num" value="1" /> |
2 <input type="checkbox" name="num" value="2" /> |
3 <input type="checkbox" name="num" value="3" /> 
Run Code Online (Sandbox Code Playgroud)

示例代码

将包含元素中的所有相互排除的复选框分组

JavaScript

$.fn.mutuallyExcludedCheckBoxes = function(){
    var $checkboxes = this.find("input[type=checkbox]");

    $checkboxes.click(function() {
      var $this = $(this),
          isChecked = $this.prop("checked");

      $checkboxes.prop("checked", false);
      $this.prop("checked", isChecked);
    });
};

// select the containing element, then trigger the plugin 
// to set all checkboxes in the containing element mutually 
// excluded
$(".alpha").mutuallyExcludedCheckBoxes();
$(".num").mutuallyExcludedCheckBoxes();
Run Code Online (Sandbox Code Playgroud)

HTML

<div class="alpha">
A <input type="checkbox" value="A" /> | 
B <input type="checkbox" value="B" /> | 
C <input type="checkbox" value="C" /> 
</div>

<div class="num">
1 <input type="checkbox" value="1" /> |
2 <input type="checkbox" value="2" /> |
3 <input type="checkbox" value="3" /> 
</div>
Run Code Online (Sandbox Code Playgroud)

示例代码

享受 :-)


小智 5

试试这个:

HTML

<div>
    Car: <input id="chkVehicleCar" name="chkVehicle" type="checkbox" value="Car" class="radiocheckbox">
    Moto: <input id="chkVehicleMoto" name="chkVehicle" type="checkbox" value="Moto" class="radiocheckbox">
    Byke: <input id="chkVehicleByke" name="chkVehicle" type="checkbox" value="Byke" class="radiocheckbox">
    Feet: <input id="chkVehicleFeet" name="chkVehicle" type="checkbox" value="Feet">
</div>

<span>    
    Red: <input id="chkColorRed" name="chkColor" type="checkbox" value="Red" class="radiocheckbox">
    Blue: <input id="chkColorBlue" name="chkColor" type="checkbox" value="Blue" class="radiocheckbox">
    Green: <input id="chkColorGreen" name="chkColor" type="checkbox" value="Green" class="radiocheckbox">

    Mango: <input id="chkFruitMango" name="chkFruit" type="checkbox" value="Mango" class="radiocheckbox">
    Orange: <input id="chkFruitOrange" name="chkFruit" type="checkbox" value="Orange" class="radiocheckbox">
    Banana: <input id="chkFruitBanana" name="chkFruit" type="checkbox" value="Banana" class="radiocheckbox">
</span>
Run Code Online (Sandbox Code Playgroud)

的JavaScript/jQuery的

$(':checkbox.radiocheckbox').click(function() {
    this.checked
        && $(this).siblings('input[name="' + this.name + '"]:checked.' + this.className)
                  .prop('checked', false);
});?
Run Code Online (Sandbox Code Playgroud)

互斥复选框按容器+名称+类名分组.您可以在同一容器中使用不同的组,也可以将具有相同名称的非独占复选框混合使用.JavaScript代码经过高度优化.你可以看到一个有效的例子.