How to make default checked for radio buttons in r

2020-08-09 07:46发布

How to make the radio button checked if the initial value is true?

标签: reactjs
4条回答
Emotional °昔
2楼-- · 2020-08-09 07:55

Using the defaultChecked property, available for <input type="checkbox"> and <input type="radio"> - https://reactjs.org/docs/uncontrolled-components.html#default-values

<input type="radio" name="radio-group" value="1" defaultChecked />
<input type="radio" name="radio-group" value="2" />
<input type="radio" name="radio-group" value="3" />
查看更多
唯我独甜
3楼-- · 2020-08-09 07:55

Adding a defaultChecked should be the idea here and not the checked value.

查看更多
ら.Afraid
4楼-- · 2020-08-09 07:56

Add the checked attribute to your radio button, e.g. checked={field.input.value}. [JS Bin]

查看更多
女痞
5楼-- · 2020-08-09 08:00

Sometimes the issue can be fixed by removing the name attribute and instead using a conditional checked value:

<li>
    <label>
    <input
        type="radio"
        value="medium"
        checked={this.state.size === "medium"}
        onChange={this.handleChange}
    />
    Medium
    </label>
</li>

<li>
    <label>
    <input
        type="radio"
        value="large"
        checked={this.state.size === "large"}
        onChange={this.handleChange}
    />
    Large
    </label>
</li>

Source Here: https://magnusbenoni.com/radio-buttons-react/

查看更多
登录 后发表回答