HTML 复选框不能进行太多样式化,因此最好自己制作;)
我为自己制作了一个,它不使用图像,只使用纯 CSS。因此,它是可扩展和可定制的。
获取复选框的值$('#id_of_my_custom_checkbox').val()
-> true/false
这是工作小提琴:https ://jsfiddle.net/JerryGoyal/xr62yara/8/
HTML:
<div>
<div id='mycheckbox' class='custom-checkbox'>
<div class='custom-tick'></div>
</div>
<span>I agree</span>
</div>
CSS
.custom-checkbox{
border: solid 2px orange;
height: 15px;
width: 15px;
display: inline-block;
cursor: pointer;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;
}
.custom-tick{
border: inherit;
display: inherit;
cursor: inherit;
visibility: hidden;
height: 8px;
width: 5px;
transform: rotate(45deg);
border-left: 0;
border-top: 0;
border-width: 2px;
margin: 0px 0px 3px 4px;
}
JS:
$(document).ready(function () {
$('.custom-checkbox').click(function (e) {
toggleCustomCheckbox(e.target);
})
});
function toggleCustomCheckbox(el) {
var tickEl = $(el).find('.custom-tick').addBack('.custom-tick');
if (tickEl.css('visibility') === 'hidden') {
tickEl.css('visibility', 'visible')
$('.custom-checkbox').val(true)
alert('you ticked me')
} else {
tickEl.css('visibility', 'hidden')
$('.custom-checkbox').val(false)
alert('you un-ticked me')
}
}
function setCustomCheckbox(checkboxid, val) {
var tickEl = $(checkboxid).find('.custom-tick').addBack('.custom-tick');
if (val && val === true) {
tickEl.css('visibility', 'visible')
$('.custom-checkbox').val(true)
}
else {
tickEl.css('visibility', 'hidden')
$('.custom-checkbox').val(false)
}
}