【发布时间】: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));
});
【问题讨论】:
标签: javascript jquery css checkbox twitter-bootstrap-3