使用jQuery应用if和else条件来添加和删除类

jogvjijk  于 12个月前  发布在  jQuery
关注(0)|答案(3)|浏览(193)

我创建了下面的代码,我想创建一个if/else条件,当用户单击标签时,将添加一个类,当用户取消单击时,将删除该类。我也是一样。
代码如下。

现场小提琴链接
Complete working code here
HTML

<div class="customCheckboxBtns">
<div class="ck-button"><label><input checked="checked" name="column-2" type="checkbox">1</label></div>
<div class="ck-button"><label class="selected"><input checked="checked" name="column-3" type="checkbox">2</label></div><!-- This is how it will be added dynamically -->
<div class="ck-button"><label><input checked="checked" name="column-4" type="checkbox">3</label></div>
<div class="ck-button"><label><input checked="checked" name="column-5" type="checkbox">4</label></div>
<div class="ck-button"><label><input checked="checked" name="column-6" type="checkbox">5</label></div>
</div>

CSS

.customCheckboxBtns{
    margin-bottom: 1rem;
    margin-top: 0.25rem;
    overflow:auto;
}

.ck-button {
    margin-right: 0.375rem;
    background-color:#ffffff;
    border-radius:4px;
    border:1px solid #26a69a;
    overflow:auto;
    float:left;
    font-family: 'Source Sans Pro', sans-serif;
    transition: all 0.2s ease 0s;
    color:#999999;
}

.ck-button:hover {
    background:#7dcac2;
    border:1px solid #26a69a;
    color:#26a69a;
    background-color:#ffffff;
}

.ck-button label {
    text-align:center;
    padding: 0.25rem 0.5rem;
    display:block;
    background:#26a69a;
    color:#ffffff;
    transition: all 0.2s ease 0s;
}
.ck-button label:hover{
    cursor:pointer;
}
.ck-button input:checked {
    background: #26a69a none repeat scroll 0 0; 
    color: #ffffff;
    padding: 0.25rem 0.5rem;
    transition: all 0.2s ease 0s;
}
.ck-button label:hover{
    background: #7dcac2 none repeat scroll 0 0;
    color:#ffffff;  
}
.ck-button label input {
    position:absolute;
    /*top:-2000px;*/
}
.ck-button label input[type="checkbox"] {
    position: absolute;
    visibility: hidden;
}
label.selected{
    background: #7dcac2 none repeat scroll 0 0;
      color:#ffffff;    
}

脚本

$('.ck-button label').on('click', function () {
    $(this).addClass('selected');    
});

$('.ck-button label').on('click', function () {
    $(this).removeClass('selected');
});
aiqt4smr

aiqt4smr1#

由于你试图复制复选框的行为,我可能会使用这样的东西:

$('.ck-button input').on('change', function() {
  $(this.parentNode).toggleClass('selected', this.checked);
});
.customCheckboxBtns {
  margin-bottom: 1rem;
  margin-top: 0.25rem;
  overflow: auto;
}

.ck-button {
  margin-right: 0.375rem;
  background-color: #ffffff;
  border-radius: 4px;
  border: 1px solid #26a69a;
  overflow: auto;
  float: left;
  font-family: 'Source Sans Pro', sans-serif;
  transition: all 0.2s ease 0s;
  color: #999999;
}

.ck-button:hover {
  background: #7dcac2;
  border: 1px solid #26a69a;
  color: #26a69a;
  background-color: #ffffff;
}

.ck-button label {
  text-align: center;
  padding: 0.25rem 0.5rem;
  display: block;
  background: #26a69a;
  color: #ffffff;
  transition: all 0.2s ease 0s;
}

.ck-button label:hover {
  cursor: pointer;
}

.ck-button input:checked {
  background: #26a69a none repeat scroll 0 0;
  color: #ffffff;
  padding: 0.25rem 0.5rem;
  transition: all 0.2s ease 0s;
}

.ck-button label:hover {
  background: #7dcac2 none repeat scroll 0 0;
  color: #ffffff;
}

.ck-button label input {
  position: absolute;
  /*top:-2000px;*/
}

.ck-button label input[type="checkbox"] {
  position: absolute;
  visibility: hidden;
}

label.selected {
  background: #7dcac2 none repeat scroll 0 0;
  color: #ffffff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="customCheckboxBtns">
  <div class="ck-button">
    <label>
      <input name="column-2" type="checkbox">1</label>
  </div>
  <div class="ck-button">
    <label class="selected">
      <input checked="checked" name="column-3" type="checkbox">2</label>
  </div>
  <div class="ck-button">
    <label>
      <input name="column-4" type="checkbox">3</label>
  </div>
  <div class="ck-button">
    <label>
      <input name="column-5" type="checkbox">4</label>
  </div>
  <div class="ck-button">
    <label>
      <input name="column-6" type="checkbox">5</label>
  </div>
</div>

JSFiddle:https://jsfiddle.net/zdwjnLvL/15/

gj3fmq9x

gj3fmq9x2#

以下内容应该有效:

$('.ck-button label').on('click', function (event) {
      event.preventDefault();
      $(this).toggleClass("selected"); 
 });`

https://jsfiddle.net/YvCil/zdwjnLvL/5/
尽管这不会选中复选框。当标签上有click处理程序时,它将同时为标签和复选框触发。参见Jquery click event triggers twice when clicked on html label

siotufzp

siotufzp3#

问题是单击标签时会触发两次单击处理程序。当您单击标签时,它会触发单击处理程序,但它也会向关联的输入复选框发送单击。该单击向上冒泡到它的包含元素(标签),导致单击处理程序再次触发。
您可能希望侦听input复选框上的clickchange事件,而不是标签。这对可访问性也有好处,因为它仍然会更改输入值,并允许用户使用键盘进行更改。

Javascript摘录

$('.ck-button input[type="checkbox"]').change(function() {
  $(this).closest('label').toggleClass('selected');
});

样本

$('.ck-button input[type="checkbox"]').change(function() {
  $(this).closest('label').toggleClass('selected');
});
.customCheckboxBtns {
  margin-bottom: 1rem;
  margin-top: 0.25rem;
  overflow: auto;
}

.ck-button {
  margin-right: 0.375rem;
  background-color: #ffffff;
  border-radius: 4px;
  border: 1px solid #26a69a;
  overflow: auto;
  float: left;
  font-family: 'Source Sans Pro', sans-serif;
  transition: all 0.2s ease 0s;
  color: #999999;
}

.ck-button:hover {
  background: #7dcac2;
  border: 1px solid #26a69a;
  color: #26a69a;
  background-color: #ffffff;
}

.ck-button label {
  text-align: center;
  padding: 0.25rem 0.5rem;
  display: block;
  background: #26a69a;
  color: #ffffff;
  transition: all 0.2s ease 0s;
}

.ck-button label:hover {
  cursor: pointer;
}

.ck-button input:checked {
  background: #26a69a none repeat scroll 0 0;
  color: #ffffff;
  padding: 0.25rem 0.5rem;
  transition: all 0.2s ease 0s;
}

.ck-button label:hover {
  background: #7dcac2 none repeat scroll 0 0;
  color: #ffffff;
}

.ck-button label input {
  position: absolute;
  /*top:-2000px;*/
}

.ck-button label input[type="checkbox"] {
  position: absolute;
  visibility: hidden;
}

label.selected {
  background: #7dcac2 none repeat scroll 0 0;
  color: #ffffff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="customCheckboxBtns">
  <div class="ck-button"><label><input checked="checked" name="column-2" type="checkbox">1</label></div>
  <div class="ck-button"><label class="selected"><input checked="checked" name="column-3" type="checkbox">2</label></div>
  <div class="ck-button"><label><input checked="checked" name="column-4" type="checkbox">3</label></div>
  <div class="ck-button"><label><input checked="checked" name="column-5" type="checkbox">4</label></div>
  <div class="ck-button"><label><input checked="checked" name="column-6" type="checkbox">5</label></div>
</div>

相关问题