改变checkbox或audio按钮的样式
( 我这里用jquery实现)
部分代码可以省略,参考者可以根据自己需求改一下
html代码
<div class="wrap">
<form action="">
方案一
<label for="cbox_1" flag="false" class="cbox cbox_1"></label>
<input type="hidden" id="cbox_1" value="false"/>
<br/>
<br/>
方案二
<label for="cbox_2" flag="false" class="cbox cbox_2"></label>
<input type="hidden" id="cbox_2" value="false"/>
</form>
</div>
css样式
.wrap {
width: 300px;
height: 300px;
margin: 100px auto;
box-sizing: border-box;
border: 2px solid #000;
padding: 60px;
}
.cbox {
margin-left: 30px;
vertical-align: middle;
height: 28px;
width: 28px;
display: inline-block;
font-size: 15px;
font-weight: 700;
line-height:1;
background-color: #fff;
border: 4px solid #0294FE !important;
box-sizing: border-box;
color: #fff;
cursor: pointer;
}
js代码
$(function(){
$(".cbox").on("click", function(){
if($(this).attr("flag")=="false"){
$(this).css("background-color","#0294FE");
$(this).html("√");
$(this).siblings("input").eq(0).val("true");
$(this).attr("flag","ture");
}else {
$(this).css("background-color","#fff");
$(this).html("");
$(this).siblings("input").eq(0).val("false");
$(this).attr("flag","false");
}
// 判断点击的是哪个按钮
// 如果点击的是第一个按钮则改变第二个按钮的状态
// 如果点击的是第二个按钮则改变第一个按钮的状态
if ($(this).get(0) == $(".cbox_1").get(0)){
$(".cbox_2").css("background-color","#fff");
$(".cbox_2").html("");
$(".cbox_2").siblings("input").eq(0).val("false");
$(".cbox_2").attr("flag","false");
}else {
$(".cbox_1").css("background-color","#fff");
$(".cbox_1").html("");
$(".cbox_1").siblings("input").eq(0).val("false");
$(".cbox_1").attr("flag","false");
}
});
})
整体代码如下
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>改变复选框样式</title>
<style>
.wrap {
width: 300px;
height: 300px;
margin: 100px auto;
box-sizing: border-box;
border: 2px solid #000;
padding: 60px;
}
.cbox {
margin-left: 30px;
vertical-align: middle;
height: 28px;
width: 28px;
display: inline-block;
font-size: 15px;
font-weight: 700;
line-height:1;
background-color: #fff;
border: 4px solid #0294FE !important;
box-sizing: border-box;
color: #fff;
cursor: pointer;
}
</style>
<script src="./javascript/jquery-3.1.1.min.js"></script>
</head>
<body>
<div class="wrap">
<form action="">
方案一
<label for="cbox_1" flag="false" class="cbox cbox_1"></label>
<input type="hidden" id="cbox_1" value="false"/>
<br/>
<br/>
方案二
<label for="cbox_2" flag="false" class="cbox cbox_2"></label>
<input type="hidden" id="cbox_2" value="false"/>
</form>
</div>
<script>
// checkbox 选择按钮改变
$(function(){
$(".cbox").on("click", function(){
if($(this).attr("flag")=="false"){
$(this).css("background-color","#0294FE");
$(this).html("√");
$(this).siblings("input").eq(0).val("true");
$(this).attr("flag","ture");
}else {
$(this).css("background-color","#fff");
$(this).html("");
$(this).siblings("input").eq(0).val("false");
$(this).attr("flag","false");
}
// 判断点击的是哪个按钮
// 如果点击的是第一个按钮则改变第二个按钮的状态
// 如果点击的是第二个按钮则改变第一个按钮的状态
if ($(this).get(0) == $(".cbox_1").get(0)){
$(".cbox_2").css("background-color","#fff");
$(".cbox_2").html("");
$(".cbox_2").siblings("input").eq(0).val("false");
$(".cbox_2").attr("flag","false");
}else {
$(".cbox_1").css("background-color","#fff");
$(".cbox_1").html("");
$(".cbox_1").siblings("input").eq(0).val("false");
$(".cbox_1").attr("flag","false");
}
});
})
</script>
</body>
</html>