【问题标题】:Set of radio buttons in "Javascript"“Javascript”中的单选按钮集
【发布时间】: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


【解决方案1】:

您可以使用&lt;label&gt; 标记非常轻松地做到这一点。只需使用以下语法:

<label>
    <input type="radio" id="radioBtb1" name="subject"/>
    <img id="image1" src="../image/img3.png" height="150px"/>
</label>
<label>
    <input type="radio" id="radioBtb2" name="subject"/>
    <img id="image2" src="../image/img3.png" height="150px"/>
</label>
<label>
    <input type="radio" id="radioBtb3" name="subject"/>
    <img id="image3" src="../image/img3.png" height="150px"/>
</label>

这将自动完成您正在寻找的事情。

【讨论】:

  • 值得一提的是,您还可以将&lt;label&gt;&lt;input&gt; 关联,而无需使用for 属性将它们包装在一起。例如在这种情况下,&lt;label for="radioBtb1"&gt;,因为radioBtb1inputid
  • 哦,我是智障。倒过来读……哈哈哈。以为他的意思是单选按钮点击=图像显示。
  • @lonelyman 如果您不确定 Nicholas Hazel 的意思,他指的是答案中的点数下方的复选标记。您可以通过这种方式标记对您最有帮助的答案,以便其他可能遇到同样问题的人清楚什么是最好的解决方法。
  • @lonelyman 你不再需要 JavaScript。如果您只使用此 HTML 代码,它将自动执行您想要的操作。请参阅demo Nicholas 发布的帖子,了解您需要什么才能使其正常工作。如果这仍然不起作用,请使用新部分更新您的问题,该部分显示您现在拥有的代码。
【解决方案2】:

如果你仍然想用 javascript 来做,你可以这样做。我这样做是为了让你可以看到发生的一切。

查看this working fiddle

window.onload = function () {

    var image1 = document.getElementById('image1');
    var image2 = document.getElementById('image2');
    var image3 = document.getElementById('image3');

    image1.addEventListener('click',runmewhenclicked);
    image2.addEventListener('click',runmewhenclicked);
    image3.addEventListener('click',runmewhenclicked);  

};  

function runmewhenclicked() {   

    var id = this.id;

    if(id == "image1")
    {
       document.getElementById("radioBtb1").checked = true;
    }
    else if(id == "image2")
    {
       document.getElementById("radioBtb2").checked = true;
    }
    else if(id == "image3")
    {
       document.getElementById("radioBtb3").checked = true;
    }
};

【讨论】:

  • 您只提供了一半的代码。为什么你不做另一半?
  • 对不起,我按了很快发布!
  • 您现在缺少一个结束}。另外,为什么要改变格式?在您的onload 函数中,您将{() 放在同一行,但对于runmewhenclickedifs,您将其放在单独的一行。
  • @joeytje50 我再次在我的文本编辑器中运行它以测试它是否失败。我把我的测试代码贴回到这里......抱歉,现在已经修复了!
  • 对不起。但我仍然有同样的问题。当我点击图片时,单选按钮仍未选中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-09
  • 2017-05-02
  • 1970-01-01
  • 1970-01-01
  • 2011-11-18
  • 2014-01-26
相关资源
最近更新 更多