简体   繁体   English

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

[英]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: 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语句中:

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

You're trying to compare to true , but instead you're setting the property to true . 您尝试将其true进行比较 ,但您将属性设置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) {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单选按钮管理 - 当我选择一个时,它会自动选择另一个 jQuery - Radio button management - when I choose one, it automatically selects the other jQuery Reactjs:我正在检查基于 state 的单选按钮,但在选中一个单选按钮时无法单击其他单选按钮 - Reactjs: I am checking radio button based on state but can't click other radio button when one is checked 单击一个单选按钮时更改布局形式 - change layout form when i click one radio button 当我单击单选按钮时,它的true或自动其他Span值将变为false - When i click on radio button then its true or automatically other Span value goes to false 当单击外部/其他单选按钮时,Bootstrap 4 删除折叠 - Bootstrap 4 remove collapse when click outside/other radio button 我想制作 4 个单选按钮和 1 个 go 按钮,当我检查任何一个单选按钮然后单击 go 按钮时,我想要更改 url - I want to make 4 radio button and 1 go button, I want when i check any one radio button and then click on go button then the url will be changeded 启用一个无线电组的单选按钮,并在选中时禁用另一个无线电组 - To enable Radio button of one radio group and disable the other radio group when selected 我想当我点击模态上的单选按钮(objectart)时,它应该在另一个 div(objecttype)中更改模态上的其他 div 数据(获取数据库) - i want when i click on radio button (objectart) on modal then it should change other div data (fetch database) on modal in another div(objecttype) 如何一键取消选中单选按钮 - How do i uncheck radio button in one click 当我点击编辑按钮时出现问题,所有其他元素都听点击,但我只需要我点击的那个 - A problem when i click on edit button, all the other elements listen to the click but i need just the one i click on
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM