简体   繁体   English

复选框:取消选中时选中另一个

[英]Checkbox: uncheck when on another is checked

I'm trying to use this function to uncheck a checkbox when another one is checked : 我想要使用此功能, 取消选中复选框,当一个又一个被选中

<script>
   $('input.unchecked').on('change', function() {
   $('input.unchecked').not(this).prop('checked', false);  
   });
</script>

<script>
$("#checkboxID").change(function(){
$("#tableID tr.rowEG").toggle(!this.checked); 
});

$("#checkbox1OG").change(function(){
$("#tableID tr.row1OG").toggle(!this.checked); 
});

$("#checkbox2OG").change(function(){
$("#tableID tr.row2OG").toggle(!this.checked); 
});

$("#checkbox3OG").change(function(){
$("#tableID tr.row3OG").toggle(!this.checked); 
});   
</script>

But does not works, because when you check another checkbox the table content disappears. 但是不起作用,因为当您选中另一个复选框时,表内容消失了。

This is the HTML code: 这是HTML代码:

<form class="filter">
   <input type="checkbox" id="checkboxID" class="unchecked"> EG
   <input type="checkbox" id="checkbox1OG" class="unchecked"> 1.OG
   <input type="checkbox" id="checkbox2OG" class="unchecked"> 2.OG
   <input type="checkbox" id="checkbox3OG" class="unchecked"> 3.OG
</form>

And here where I want to use it: 在这里我要使用它:

http://develop.nowcommu.myhostpoint.ch/table-test.html http://develop.nowcommu.myhostpoint.ch/table-test.html

Any ideas? 有任何想法吗?

EDIT: Maybe this is the problem? 编辑:也许这是问题吗?

<script>
$("#checkboxID").change(function(){
$("#tableID tr.rowEG").toggle(!this.checked); 
});

    $("#checkbox1OG").change(function(){
$("#tableID tr.row1OG").toggle(!this.checked); 
});

    $("#checkbox2OG").change(function(){
$("#tableID tr.row2OG").toggle(!this.checked); 
});

    $("#checkbox3OG").change(function(){
$("#tableID tr.row3OG").toggle(!this.checked); 
});   
</script>

Here is a simple solution without the need of script. 这是一个不需要脚本的简单解决方案。

 input{ display:none; } label{ width:25px; height:25px; font-size:16px; cursor:pointer; position:relative; padding-left: 30px; } label ~ label { margin-left: 40px; } label:before, label:after{ top: 0; left: 0; position:absolute; height:15px; width:15px; content:' '; border-radius: 2px; border: 1px #3a3a3a solid; font-weight: bold; } input:checked + label:after{ top: -2px; left: 2px; content: '\\2713'; border: none; } 
 <form class="filter"> <input type="radio" name="radio" id="radio"><label for="radio"> EG </label> <input type="radio" name="radio" id="radio1"><label for="radio1"> 1.OG </label> <input type="radio" name="radio" id="radio2"><label for="radio2"> 2.OG </label> <input type="radio" name="radio" id="radio3"><label for="radio3"> 3.OG </label> </form> 

See the fiddle: https://jsfiddle.net/61eywpzg/ 看到小提琴: https : //jsfiddle.net/61eywpzg/

HTML 的HTML

<form class="filter">
   <input type="checkbox" id="checkboxID" class="unchecked"> EG
   <input type="checkbox" id="checkbox1OG" class="unchecked"> 1.OG
   <input type="checkbox" id="checkbox2OG" class="unchecked"> 2.OG
   <input type="checkbox" id="checkbox3OG" class="unchecked"> 3.OG
</form>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>

jQuery jQuery的

$('input.unchecked').on('change', function() {
    $('input.unchecked').not(this).prop('checked', false);  
});

Try the below snippet: 尝试以下代码段:

 $('.unchecked').click(function(){ $(this).siblings().prop("checked",false) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form class="filter"> <input type="checkbox" id="checkboxID" class="unchecked"> EG <input type="checkbox" id="checkbox1OG" class="unchecked"> 1.OG <input type="checkbox" id="checkbox2OG" class="unchecked"> 2.OG <input type="checkbox" id="checkbox3OG" class="unchecked"> 3.OG </form> 

You can use radio buttons instead of checkboxes. 您可以使用单选按钮代替复选框。 To do this, change the type of your input to radio, example: 为此,请将输入类型更改为单选,例如:

<input type="radio" id="checkboxID" class="unchecked">

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

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