我似乎无法使checkbox.checked工作,我有一个基于onclick()更改图像的脚本但是我希望它根据是否选中复选框来更改它。我究竟做错了什么?此外,如果有人可以对此进行处理,则复选框不会检入ie8
这是实时链接:http://jsbin.com/uburan/1/edit
代码:
<script type="text/javascript">
function func()
{
var img1= document.getElementById("img1");
if(checkbox.checked)
{
img1.src = "images/" + "img1a.jpg";
img1.name = "off";
}
else
{
img1.src = "images/" + "img1.jpg";
img1.name = "on";
}
}
</script>
</head>
<body>
<form>
<p align="center">
<input type="checkbox" name="interest1" id="interest1" value="x">
<input type="checkbox" name="interest2" id="interest2" value="x">
<input type="checkbox" name="interest3" id="interest3" value="x"></p>
<p align="center">
<label for="interest1" id="label-interest1"><img src="images/img1.jpg" width="781" height="800" onclick="func()" id="img1" /></label>
<label for="interest2" id="label-interest2"><img src="/images/img2.jpg" width="781" height="800" /></label>
<label for="interest3" id="label-interest3"><img src="/images/img3.jpg" width="781" height="800" /></label></P><!-- code making checkbox be an image-->
</form>
答案 0 :(得分:2)
您必须指定复选框的ID(或其在DOM中的引用)。像这样:
if(document.getElementById('interest1').checked)
{
// blah
}
如果你想检查它们是否全部被检查过:
if(document.getElementById('interest1').checked &&
document.getElementById('interest2').checked &&
document.getElementById('interest3').checked) {
// blah
}