【问题标题】:How to uncheck all checkboxes, if one checkbox checked?如果选中一个复选框,如何取消选中所有复选框?
【发布时间】:2015-05-31 09:55:34
【问题描述】:

我有一组复选框,如下所示:

<label class="checkbox-inline">
  <input type="checkbox" id="" value="1"> information
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="" value="2"> information
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="" value="3"> information
</label>        
<label class="checkbox-inline">
  <input type="checkbox" id="" value="4"> any
</label>

使用这些复选框,用户可以进行选择。但是,如果用户选择了 "any" 复选框,那么我需要取消选择所有其他选择。

我尝试过类似的方法,但它对我不起作用。

$('.checkbox-inline').click(function(){
      $("input[type='checkbox']").attr('checked', false);
      $(this).attr('checked', true);
})

谁能告诉我如何在 jquery 中做到这一点?

注意:我正在为我的复选框使用动态生成的 HTML。

【问题讨论】:

  • 你的功能最好用正确的类型来实现,即收音机
  • 是的,如果这是您需要的行为,请使用单选按钮。您将完全不用 JS,获得更好的性能并且不会破坏 WYSIWYG 模型,因为用户期望复选框允许多个选择,而单选按钮只允许一个选择。 UX(用户体验)会更好!
  • @mplungjan,我不能为此使用收音机。因为如果用户想要选择许多复选框,应该这样做。这意味着如果用户想要,可以选择第一个三个复选框(参见关于 html)。但是如果用户选择第四个“任何”其他应该被取消选中....清除?
  • 看看我的回答,这很有趣,为什么我们大多数人都指出了错误的问题:) 顺便说一句,包括我自己 :)

标签: javascript jquery html checkbox


【解决方案1】:

id 定义为任意checkbox,如下所示:

<label class="checkbox-inline">
  <input type="checkbox" id="" value="1"> information
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="" value="2"> information
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="" value="3"> information
</label>        
<label class="checkbox-inline">
  <input type="checkbox" id="any-checkbox" value="4"> any
</label>

我建议这样做是因为使用它是第四个事实并不是一个好主意。如果你以后在它之前添加另一个复选框,那么它将不再是第四个。

    //We use .on to tackle with the dynamic nature of the HTML
    $( "body" ).on( "change", ".checkbox-inline > input[type=checkbox]", function() {
        if ($(this).attr("id") === "any-checkbox") { //Any checkbox tick
            if ($(this).prop("checked")) { //User checked any
                //Other checkboxes are unchecked
                $(".checkbox-inline > input[type=checkbox]:not(#any-checkbox)").prop("checked", false)
            }
        } else { //Other checkbox tick
            if ($(this).prop("checked")) {//User checked another checkbox
                //Any checkbox is unchecked
                $("#any-checkbox").prop("checked", false);
            }
        }
    });

编辑:根据评论,我已经解决了多个组的问题,如下所示:

<label class="checkbox-inline">
  <input type="checkbox" id="" name="hairColor" value="1"> information
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="" name="hairColor" value="2"> information
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="" name="hairColor" value="3"> information
</label>        
<label class="checkbox-inline">
  <input type="checkbox" id="hairColor" name="hairColor" value="4" class="any"> any
</label>
   <br> 
<label class="checkbox-inline">
  <input type="checkbox" id="" name="eyeColor" value="1"> eyeColor
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="" name="eyeColor" value="2"> eyeColor
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="" name="eyeColor" value="3"> eyeColor
</label>        
<label class="checkbox-inline">
  <input type="checkbox" id="eyeColor" name="eyeColor" class="any" value="4"> any
</label>

//We use .on to tackle with the dynamic nature of the HTML
$( "body" ).on( "change", ".checkbox-inline > input[type=checkbox]", function() {
    if ($(this).hasClass("any")) { //Any checkbox tick
        if ($(this).prop("checked")) { //User checked any
            //Other checkboxes are unchecked
            $(".checkbox-inline > input[type=checkbox][name=" + $(this).attr("name") + "]:not(.any)").prop("checked", false)
        }
    } else { //Other checkbox tick
        if ($(this).prop("checked")) {//User checked another checkbox
            //Any checkbox is unchecked
            $("[name=" + $(this).attr("name") + "].any").prop("checked", false);
        }
    }
});

【讨论】:

  • @MCC,你是对的,我有一个错字,id 不在引号中。编辑了我的答案。看到这个小提琴:jsfiddle.net/6D9kS/161
  • 还有一个问题。如果我在不同的不同组下有复选框,则此代码如何修改。检查这个小提琴 - jsfiddle.net/6D9kS/163
  • 我已经改变了一点这个和一点点那个。主要思想是将组显示为类的一部分,并且任何组也被标记为类,因为它在页面上不再是唯一的。
  • 我不确定取消标记答案是否足够,因为它回答了您最初的问题。但是,无论如何,我已经为您解决了这个问题。这是:jsfiddle.net/6D9kS/166
【解决方案2】:

试试这个,满足你的要求

$(document).on("change", "input", function() {
  if ($("input[value=4]").is(":checked")) {
    $("input[value!=4]").attr("checked", false);

  }

});

$(document).ready(function() {
  for (i = 1; i < 4; i++) {
    $("#container").append("<label class='checkbox-inline'>  <input type='checkbox' id='' value='" + i + "'>information</label>")
  }
  $("#container").append("<label class='checkbox-inline'>  <input type='checkbox' id='' value='4'>any</label>");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div id="container"></div>

【讨论】:

  • @Alex,将您的答案更改为委托。如果您不知道该怎么做,请告诉我
  • @AmmarCSE,没关系。但value="4" 可能与动态信息不同
  • @MCC,这就是为什么您必须为“任何”标签插入一个特殊类,然后定位它
  • @AmmarCSE,您是否删除了您的答案?
  • @MCC,是的,如果你想从中得到什么,我可以取消删除它
【解决方案3】:

您可以使用此 jquery 代码,其中 input_id 将是您的复选框的 ID。

$(document).on('click', "#input_id :checkbox", function() {
    if ($(this).is(':checked')) {
        $("#input_id :checkbox").prop('checked', false);
        $(this).prop('checked', true);
    }
})

【讨论】:

    【解决方案4】:

    您好,我编辑了我的答案,

    有趣的是,我们大多数人都专注于标签的点击,但试图捕捉的是复选框@.@的点击事件。@

    我通过修改你的代码来完成这项工作:

    <label class="checkbox-inline">
      <input class ="checkbox1" type="checkbox" id="" value = "1" > information
    </label>
    <label class="checkbox-inline">
      <input class ="checkbox1" type="checkbox" id="" value = "2"> information
    </label>
    <label class="checkbox-inline">
      <input class ="checkbox1" type="checkbox" id="" value = "3"> information
    </label>        
    <label class="checkbox-inline">
      <input class ="checkbox1" type="checkbox" id="" value = "4"> any
    </label>
    
    
    $('.checkbox1').click(function(){
          $("input[type='checkbox']").removeAttr('checked');
          $(this).prop('checked', true);
    })
    

    我为复选框添加了单独的类并捕获了它的点击事件。 有用!! :P

    编辑: 如果“任意”复选框有特殊逻辑,

    $('.checkbox1').click(function(){
        if ($(this).val() != 4 && $("input[value=4]").is(":checked"))
            return false;
        // Check if any was checked
        if ($(this).val() == 4) 
          $("input[value!=4]").removeAttr('checked');
        //$(this).prop('checked', true);
    })
    

    两个场景被困。 1.如果选中任何复选框,则取消选中其他复选框 2.如果有其他复选框,则不允许检查其他复选框

    P.S 我重用了@Alex 回答中的一些代码:D

    【讨论】:

    • 一次只能勾选一个复选框
    • 你想达到什么目的?我可以帮你修改
    猜你喜欢
    • 2014-05-23
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    相关资源
    最近更新 更多