【发布时间】:2014-01-27 06:55:06
【问题描述】:
我有一组三个单选按钮与一组三个图像相连 - 即 {image1, radioBtn1}, {image2, radioBtn2}, {image3, radioBtn3}。
我正在尝试执行以下操作:当我点击一张图片时,连接到该按钮的单选按钮自动变为“选中”(例如:当我点击 image3 时,radioBtn3 将被选中)
我试过以下代码:
HTML:
<input type="radio" id="radioBtb1" name="subject"/><a href="#"><img id="image1" src="../image/img3.png" height="150px"/></a>
<input type="radio" id="radioBtb2" name="subject"/><a href="#"><img id="image2" src="../image/img3.png" height="150px"/></a>
<input type="radio" id="radioBtb3" name="subject"/><a href="#"><img id="image3" src="../image/img3.png" height="150px"/></a>
Javascript:
function selectSubject(){
if(document.getElementById('image1'))
document.getElementById(radioBtb1).checked=true;
if(document.getElementById('personal'))
document.getElementById(radioBtb2).checked=true;
if(document.getElementById('image3'))
document.getElementById(radioBtb3).checked=true;
}
但这根本不起作用。
【问题讨论】:
-
神圣格式...所以你想要 3 个按钮,3 个图像,并且只有图像应该根据你选择按钮而改变?
-
如果您创建标签并根据需要在每个标签上设置背景图像会更容易。那你就不需要任何JS了。
-
单选按钮。当我按下一个图像时,连接到该图像的重做按钮将变为选中。
-
No No 这不会更容易,因为我的网页包含大尺寸图片
-
if(document.getElementById('image1'))– 你希望它做什么?这只检查文档中是否存在具有该 id 的元素 exists;它与该元素是否被点击无关。
标签: javascript jquery html image