如果没有用户给定的值React如何提交defaultValue

因此,我在 React 应用程序的表单中有此复选框,它将 defaultChecked 值设置为对象的“静态值”

<input type='checkbox' name='static' defaultChecked={obj.static}/>

现在,每当我提交表单时,它都不会从 defaultChecked 中读取,我必须更改它,以便它甚至可以从复选框中读取。

有没有什么办法解决这一问题?谢谢。


回首忆惘然
浏览 94回答 1
1回答

catspeake

defaultChecked 不建议使用。我会尝试将该信息存储在如下状态:https://codesandbox.io/s/holy-snow-l3u6bconst obj = {&nbsp; defaultValue: true};class Test extends React.Component {&nbsp; constructor() {&nbsp; &nbsp; super();&nbsp; &nbsp; this.state = {&nbsp; &nbsp; &nbsp; checked: obj.defaultValue&nbsp; &nbsp; };&nbsp; &nbsp; this.onStaticChangeHandler = e => {&nbsp; &nbsp; &nbsp; this.setState({&nbsp; &nbsp; &nbsp; &nbsp; checked: e.target.checked&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; };&nbsp; }&nbsp; render() {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <input&nbsp; &nbsp; &nbsp; &nbsp; type="checkbox"&nbsp; &nbsp; &nbsp; &nbsp; name="static"&nbsp; &nbsp; &nbsp; &nbsp; onChange={this.onStaticChangeHandler}&nbsp; &nbsp; &nbsp; &nbsp; checked={this.state.checked}&nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; );&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5