【问题标题】:Custom CSS checkbox jQuery-induced "checked" property doesn't visually "check" the checkbox自定义 CSS 复选框 jQuery 引发的“选中”属性不会在视觉上“选中”复选框
【发布时间】:2016-04-13 15:31:13
【问题描述】:

首先,抱歉这个含糊的问题,我不知道如何用语言表达。我的问题最好用一个例子来说明。

我正在使用一个带有多个复选框的引导下拉菜单。我使用了一个 css 技巧来改变它们的外观,使用了一些漂亮的 FontAwesome 图标。但是,当我尝试使用 jQuery 对这些复选框的状态进行操作时,第一次选中/取消选中似乎可以工作,但是任何后续切换(选中/取消选中)都不起作用......

$('.js-inputhit').click(function() {
  if (this === event.target) {
    $(this).find(':input').click();
  }
});
$('.js-inputhit :input').click(function(e) {
  e.stopPropagation();
});

$('.selectchat_all').click(function() {
  $('.selectchat_active').attr('checked', false);
  $('#chat1, #chat2, #chat3, #chat4').attr('checked', true);
});
$('.selectchat_active').click(function() {
  $('.selectchat_all').attr('checked', false);
  $('#chat1, #chat2, #chat3, #chat4').attr('checked', false);
});
/*Adding custom checkbox icons*/

label {
  position: relative;
  padding-left: 20px;
  cursor: pointer;
}
label:before,
label:after {
  font-family: FontAwesome;
  font-size: 14px;
  /*absolutely positioned*/
  position: absolute;
  top: 0;
  left: 0;
}
label:before {
  content: '\f096';
  /*unchecked*/
}
label:after {
  content: '\f046';
  /*checked*/
  /*checked icon will be hidden by default by using 0 max-width and overflow hidden*/
  max-width: 0;
  overflow: hidden;
  opacity: 0.5;
  /*CSS3 transitions for animated effect*/
  transition: all 0.35s;
}
/*hiding the original checkboxes*/

input[type="checkbox"] {
  display: none;
}
/*when the user checks the checkbox the checked icon will animate in*/

input[type="checkbox"]:checked + label:after {
  max-width: 25px;
  /*an arbitratry number more than the icon's width*/
  opacity: 1;
  /*for fade in effect*/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" />
<input type="checkbox" name="chat1" id="chat1" checked/>
<label for="chat1" style="font-weight: normal; margin-bottom: 0px;">DreamhackCS</label>
<br>

<input type="checkbox" name="chat2" id="chat2" />
<label for="chat2" style="font-weight: normal; margin-bottom: 0px;">IzakOOO</label>
<br>

<input type="checkbox" name="chat3" id="chat3" />
<label for="chat3" style="font-weight: normal; margin-bottom: 0px;">Nightblue3</label>
<br>

<input type="checkbox" name="chat4" id="chat4" />
<label for="chat4" style="font-weight: normal; margin-bottom: 0px;">imaqtpie</label>
<br><br>

<input type="checkbox" name="active" class="selectchat_active" id="active" />
<label for="active" style="font-weight: normal; margin-bottom: 0px;">Check none</label>
<br>

<input type="checkbox" name="all" class="selectchat_all" id="all"/>
<label for="all" style="font-weight: normal; margin-bottom: 0px;">Check all</label>

本例中的问题: 单击“检查所有”会检查所有 4 个通道。单击“不选中”将取消选中所有这些。 (到目前为止,一切都很好)。但是,在那之后,“检查所有”不再起作用。它不再检查 4 个通道。 “全选”仍然取消选中所有这些(如果您手动检查它们),但“全选”不再有效...

谁能找到我的问题:)?非常感谢您的帮助。

【问题讨论】:

    标签: javascript jquery html css checkbox


    【解决方案1】:

    使用 .prop() 代替 .attr()

    attr() 只更新实际的 DOM 树

    有关详细信息,请参阅http://api.jquery.com/prop/this answer

    $('.js-inputhit').click(function() {
      if (this === event.target) {
        $(this).find(':input').click();
      }
    });
    $('.js-inputhit :input').click(function(e) {
      e.stopPropagation();
    });
    
    $('.selectchat_all').click(function() {
      $('.selectchat_active').prop('checked', false);
      $('#chat1, #chat2, #chat3, #chat4').prop('checked', true);
    });
    $('.selectchat_active').click(function() {
      $('.selectchat_all').prop('checked', false);
      $('#chat1, #chat2, #chat3, #chat4').prop('checked', false);
    });
    /*Adding custom checkbox icons*/
    
    label {
      position: relative;
      padding-left: 20px;
      cursor: pointer;
    }
    label:before,
    label:after {
      font-family: FontAwesome;
      font-size: 14px;
      /*absolutely positioned*/
      position: absolute;
      top: 0;
      left: 0;
    }
    label:before {
      content: '\f096';
      /*unchecked*/
    }
    label:after {
      content: '\f046';
      /*checked*/
      /*checked icon will be hidden by default by using 0 max-width and overflow hidden*/
      max-width: 0;
      overflow: hidden;
      opacity: 0.5;
      /*CSS3 transitions for animated effect*/
      transition: all 0.35s;
    }
    /*hiding the original checkboxes*/
    
    input[type="checkbox"] {
      display: none;
    }
    /*when the user checks the checkbox the checked icon will animate in*/
    
    input[type="checkbox"]:checked + label:after {
      max-width: 25px;
      /*an arbitratry number more than the icon's width*/
      opacity: 1;
      /*for fade in effect*/
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" />
    <input type="checkbox" name="chat1" id="chat1" checked/>
    <label for="chat1" style="font-weight: normal; margin-bottom: 0px;">DreamhackCS</label>
    <br>
    
    <input type="checkbox" name="chat2" id="chat2" />
    <label for="chat2" style="font-weight: normal; margin-bottom: 0px;">IzakOOO</label>
    <br>
    
    <input type="checkbox" name="chat3" id="chat3" />
    <label for="chat3" style="font-weight: normal; margin-bottom: 0px;">Nightblue3</label>
    <br>
    
    <input type="checkbox" name="chat4" id="chat4" />
    <label for="chat4" style="font-weight: normal; margin-bottom: 0px;">imaqtpie</label>
    <br><br>
    
    <input type="checkbox" name="active" class="selectchat_active" id="active" />
    <label for="active" style="font-weight: normal; margin-bottom: 0px;">Check none</label>
    <br>
    
    <input type="checkbox" name="all" class="selectchat_all" id="all"/>
    <label for="all" style="font-weight: normal; margin-bottom: 0px;">Check all</label>

    【讨论】:

    • 谢谢!我不知道 :) 现在可以了。我会尽可能(从现在起 7 分钟)将您的答案选为正确的。
    • 很高兴它解决了这个问题。这就是stackoverflow的用途:)
    【解决方案2】:

    尝试使用.prop 而不是.attr

    $('#chat1, #chat2, #chat3, #chat4').prop('checked', true);
    

    有关更多信息,请参阅this answer

    【讨论】:

    • 太好了,这行得通。感谢您澄清差异。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 2014-07-27
    相关资源
    最近更新 更多