简体   繁体   中英

When I try to click one radio button it selects the other

So, when I click the 'Submit' button and I have the 'no' radio button selected it selects the 'yes' radio button..

Script:

    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>

The problem is in your if statements:

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

You're trying to compare to true , but instead you're setting the property to true . The = operator is for assignment; you're looking for the == or === operators, though in this case you really just need to check the property as a boolean:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM