【发布时间】:2015-07-18 00:29:56
【问题描述】:
使用method 1 to create a clickable label、hiding the checkbox with CSS 和toggling it and the label with jQuery,我得到了这个:
$('label').click(function(e) {
$(this).toggleClass('active');
var cbx = $('input', this);
cbx.prop('checked', !cbx.prop("checked"));
console.log($('input', this).prop('checked'));
return false;
})
input[type="checkbox"] {visibility: hidden;position: absolute}
label {background-color: pink;border: 1px solid lightblue;padding: 20px}
label.active {background-color: lightgreen}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<input type="checkbox" checked />Check Button</label>
我的问题是:是否可以仅使用 CSS 来做同样的事情?
我意识到我使用的是<label> 而不是<button>,并且我认为有更好的方法可以使用jQuery 来实现这一点。如果您考虑回复一个,请使用复选框(或至少解释为什么不能使用它)。
【问题讨论】:
标签: jquery html css checkbox input