【发布时间】:2019-12-08 23:12:44
【问题描述】:
我希望能够取消选中单选按钮,想法是这样的:如果我单击某个单选按钮,它将被选中,如果我单击另一个字段,则另一个字段将被选中但是如果我单击已选中的字段,我希望取消选中它,以便所有字段为空。我试图捕捉被选中或未选中的时刻,但似乎与复选框相反,单选按钮没有此字段。有人知道如何实现吗?
setTests = (key, e) => {
console.log(e.checked)
if (e.checked) {
// this.setState({[key]: null})
console.log('works')
}
}
RadioGroup
value={this.state.test_mode}
style={{ display: "block" }}
onChange={e => this.setTests({ "test_mode", e.target })}
>
<FormControlLabel value="before" control={<Radio color="primary"/>} label="before tests" />
<FormControlLabel value="progressing" control={<Radio color="primary"/>} label="progressing" />
<FormControlLabel value="done" control={<Radio color="primary"/>} label="done" />
</RadioGroup>
【问题讨论】:
-
是的,对于单选按钮,您无法通过单击取消选中单选按钮。
标签: reactjs radio-button material-ui