【发布时间】:2021-12-29 22:28:20
【问题描述】:
我的一个反应组件中有这个表单
<div className="form-check">
<input
type="radio"
className="form-check-input"
value={3}
name="priority"
/>
<label className="form-check-label">High Priority</label>
</div>
<div className="form-check">
<input
type="radio"
value={2}
className="form-check-input"
name="priority"
/>
<label className="form-check-label">Medium Priority</label>
</div>
<div className="form-check">
<input
type="radio"
value={1}
className="form-check-input"
name="priority"
/>
<label className="form-check-label">Low Priority</label>
</div>
然后我在按钮上有一个 onclick 功能。我想在我的函数中获取单选表单的值。我怎么做?有没有办法使用 useRef 挂钩? 注意:我使用的是函数式组件,所以任何带有类组件的解决方案都无济于事。
谢谢!
【问题讨论】:
-
这能回答你的问题吗? Uncheck radio buttons in react
-
使用状态来维护输入值,然后在调用时访问函数中的状态。
-
我在哪里说单选值等于状态?我会在每个无线电输入中说明它吗?
标签: javascript reactjs forms input react-hooks