当我尝试单击一个单选按钮时,它会选择另一个

时间:2015-07-25 12:09:16

标签: javascript html dom

所以,当我点击提交'按钮,我有'没有'单选按钮选择“是”'单选按钮..

脚本:

    function action() {
        var name = document.getElementById('username').value;
        document.getElementById("theIMG").src = "https://crafatar.com/avatars/" + name;
        console.log("Working");

        if (document.getElementById('render_Yes').checked = true) {
            document.getElementById("renderIMG").src = "https://crafatar.com/renders/body/" + name + "?helm&scale=4";
        } else {
            if (document.getElementById('render_No').checked = true) {
                return false;
            }
        }
    }

HTML:

Render: <br/>
<input type="radio" name="yn" id="render_Yes" value="yes">Yes</input>
<input type="radio" name="yn" id="render_No" value="no" checked="checked">No</input> <br/> <br/>

<button id="submit" onClick="action()">Submit</button> <br />
<br />
<img id="theIMG"></img>
<img id="renderIMG"></img>

1 个答案:

答案 0 :(得分:1)

问题出在您的if声明中:

    if (document.getElementById('render_Yes').checked = true) {

您正在尝试true进行比较,而是属性设置为true=运算符用于分配;您正在寻找=====运算符,但在这种情况下,您只需要将属性检查为布尔值:

    if (document.getElementById('render_Yes').checked) {