简体   繁体   English

jQuery-单击复选框添加类和删除类

[英]jQuery - Click checkbox add class and remove class

I am using bootstrap to create a checkbox and I want to create a checkbox that clicks on all checkboxes and vice versa (I know there are a lot of guides in stackoverflow about this) but in all it only works on the inputs. 我正在使用引导程序创建一个复选框,并且想要创建一个单击所有复选框的复选框,反之亦然(我知道在stackoverflow中有很多关于此的指南),但总的来说,它仅适用于输入。

In this case I am enclosing the input with a label "label", when activating the checkbox bootstrap automatically adds the "active" class to this tag that causes the "check" to appear. 在这种情况下,我用标签“ label”将输入括起来,当激活复选框引导程序时,会自动将“ active”类添加到该标签中,从而导致“ check”出现。 When I tried to implement scripts found here that added the "checked" attribute to the input of the checkbox, they did not work for this very reason so I tried the following: 当我尝试实现此处找到的将“ checked”属性添加到复选框输入的脚本时,由于这个原因它们无法正常工作,因此我尝试了以下操作:

$('label#seleccionartodos').click(function(){
  $('input.videoscheck').prop('checked', true).parent().addClass('active');
}); 

And this is my html: 这是我的html:

<div class="form-group" data-toggle="buttons">
<label for="selectall">SELECT / UNSELECT ALL:</label>
<label class="btn btn-success" id="seleccionartodos">
  <input type="checkbox" id="selectall" autocomplete="off">
  <span class="glyphicon glyphicon-ok"></span>
</label>
</div>
<div class="form-group" data-toggle="buttons">
  <label class="btn btn-success">
    <input type="checkbox" class="videoscheck" autocomplete="off">
    <span class="glyphicon glyphicon-ok"></span>
  </label>
 </div>
 <div class="form-group" data-toggle="buttons">
  <label class="btn btn-success">
    <input type="checkbox" class="videoscheck" autocomplete="off">
    <span class="glyphicon glyphicon-ok"></span>
  </label>
 </div>
 <div class="form-group" data-toggle="buttons">
  <label class="btn btn-success">
    <input type="checkbox" class="videoscheck" autocomplete="off">
    <span class="glyphicon glyphicon-ok"></span>
  </label>
 </div>

This works halfway, when I click the checkbox, add the "checked" attribute to all the checkboxes with the ".videoscheck" class and also adds the "active" class to the parent tag that encloses them. 这工作一半,当我单击复选框时,将“ checked”属性添加到带有“ .videoscheck”类的所有复选框,还将“ active”类添加到包围它们的父标记。

But since I'm not good with jQuery, I do not know how to do it so that when I click it again, all other checkboxes will be deselected (that is, the opposite of the initial action). 但是由于我对jQuery不满意,所以我不知道该怎么做,因此当我再次单击它时,将取消选择所有其他复选框(即与初始操作相反)。

Here's a Fiddle showing what I'm exposing. 这是一个小提琴,显示了我要暴露的东西。

$('label#seleccionartodos').click(function(){
     $('input.videoscheck').prop('checked', $(this).children('input').is(':checked'))
        .parent().toggleClass('active');
});

But I prefer this way: ( you already bound label to checkbox, so you can deal with change event of checkbox ) 但是我更喜欢这样:( 您已经将标签绑定到复选框,因此您可以处理复选框的change事件

$('#selectall').on('change', function() {
    $('input.videoscheck').prop('checked', $(this).is(':checked')).parent().toggleClass('active');
});

I think it's better to bind a change event on the checkbox itself: 我认为最好将更改事件绑定到复选框本身:

$('input#selectall').change(function(){
  if (this.checked) {
    $('input.videoscheck').prop('checked', true).parent().addClass('active');
  } else {
    $('input.videoscheck').prop('checked', false).parent().removeClass('active');
  }
});

jQuery has a toggleClass function: jQuery具有toggleClass函数:

$('div').click(function() {
  $(this).toggleClass('active');
}

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

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