【问题标题】:Boostrap glyphicon checkbox, jquery to check box引导 glyphicon 复选框,jquery 到复选框
【发布时间】:2017-10-28 06:05:56
【问题描述】:

提前致谢。我非常接近使用 css、boostrap 和 javascript 获得一个标签来检查一个复选框。问题是当第一次点击时,jquery 工作但css 没有。只有在第一次之后才会起作用。

html

   <label class="checkbox">
      <input type="checkbox">
      <span class="glyphicon"></span>
    </label>

css

label.checkbox input[type="checkbox"]  {
  display:none;
}

label.checkbox span  {
  margin:-4px auto;
  cursor:pointer;
  border:2px solid #BBB;
  border-radius:100px;
  width:120px;
  height:120px;
  background:#f33;
  position: relative;
  transition:background 0.4s;
}

label.checkbox span:before,
label.checkbox :checked + span:after  {
  position:absolute;
  font-size:100px;
  top: 8px;
  left:8px;
}

label.checkbox span:before  {
  content: "\e088";
  color:#333;
}

label.checkbox :checked + span:before  {
  visiblity:hidden;

}
label.checkbox span:after  {
  content:'';
}
label.checkbox :checked + span {
  width:120px;
  height:120px;
  background:#3f3;
}
label.checkbox :checked + span:after {
  content: '\e089';
  color: #333;
}

jquery

//if checkbox becomes checked
  $('label span').on('click', function() {
    check = $(this).parent().find('input:checkbox');
//is checked true = uncheck, false = check
   (check.prop('checked') ? check.attr('checked', false) : check.attr('checked', true));
  });

jsfiddle

【问题讨论】:

    标签: javascript jquery css checkbox twitter-bootstrap-3


    【解决方案1】:

    第一次,当您单击标签时,复选框的值设置为 false,因此根据您的三元运算符,它不会显示更改。

    你需要这样做才能看到变化

     $(document).ready(function () {
       $('input[type="checkbox"]').on('change', function() {
       ($(this).is(":checked") ? $(this).attr('checked', true) :     $(this).attr('checked', false));
       });
    });
    

    这里可以看到效果jsfiddle

    【讨论】:

    • 感谢您修复了第一次点击更改,但如果您检查复选框以将更改更改为“已选中”,它仍然只会在第二次操作时发生。
    【解决方案2】:

    你可以使用这个javascript

        $('label span').on('click', function() {
    
        check = $(this).prev().find('input:checkbox');
    //is checked true = uncheck, false = check
        (check.prop('checked') ? check.attr('checked', false) : check.attr('checked', true));
      });
    

    【讨论】:

    • 修复了第一次单击以进行更改,但在您检查元素时,它不会在多个操作之前将“选中”添加到复选框。
    猜你喜欢
    • 1970-01-01
    • 2013-12-15
    • 2014-07-10
    • 1970-01-01
    • 2014-09-28
    • 2014-08-08
    • 2014-05-30
    • 1970-01-01
    • 2022-06-15
    相关资源
    最近更新 更多