我正在尝试根据复选框的选中/未选中状态更改表单中每个复选框中标签的背景颜色。到目前为止,我最初已经更改了它,但是当我取消选中时它不会变回来:
javascript:
function statecheck(layer) {
var myLayer = document.getElementById(layer);
if(myLayer.checked = true){
myLayer.style.backgroundColor = "#bff0a1";
} else {
myLayer.style.backgroundColor = "#eee";
};
}
html:
<form action="" method="get">
<label title="Alabama" id="Alabama"><input type="checkbox" value="checkbox" onchange="statecheck('Alabama')" />AL</label>
<label title="Alaska" id="Alaska"><input type="checkbox" value="checkbox" onchange="statecheck('Alaska')" />AK</label>
<label title="American Samoa" id="AmericanSamoa"><input type="checkbox" value="checkbox" onchange="statecheck('AmericanSamoa')" />AS</label>
</form>
CSS:
label {
margin:0px 2px 4px 2px;
padding: 1px;
background-color: #eee;
display: block;
width: 50px;
}