You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1.input 相同的name会作为一组操作,例如checkbox; 在react中多次调用, 如果不区分name 和 id 默认只会生效第一个checkbox; name在input中一般是form提交时才使用
<div><labelhtmlFor="apple"><inputid="apple"type="checkbox"name="demo"/> apple
</label><labelhtmlFor="orange"><inputid="orange"type="checkbox"name="demo"/> orange
</label></div>
2.defaultChecked, defaultValue 是react的属性,在HTML中input只对 checked 和 value 生效。defaultChecked不能渲染后端传入的数据,且后端数据可能会变。两种方式可以解决:
在react中使用表单元素要注意以下几种情况:
1.input 相同的name会作为一组操作,例如checkbox; 在react中多次调用, 如果不区分name 和 id 默认只会生效第一个checkbox; name在input中一般是form提交时才使用
2.defaultChecked, defaultValue 是react的属性,在HTML中input只对 checked 和 value 生效。defaultChecked不能渲染后端传入的数据,且后端数据可能会变。两种方式可以解决:
总结:
react推荐使用你的表单元素是controlled,所以尽量保持它是可控,推荐使用第一种,且改变表单元素的值状态用
setState
去操作。参考:
react可控组件:https://reactjs.org/docs/forms.html#controlled-components
The text was updated successfully, but these errors were encountered: