如果你想在React中预填充表单或使用自动填充功能,但发现它不起作用,可以尝试以下解决方法:
handleChange(event) {
this.setState({ username: event.target.value });
}
确保自动填充的数据与表单元素的name属性匹配。
在表单元素上添加autoComplete属性,并设置为正确的值(例如,"on"表示启用自动填充)。
class MyForm extends React.Component {
constructor(props) {
super(props);
this.usernameRef = React.createRef();
}
componentDidMount() {
this.usernameRef.current.value = this.props.preFilledValue;
}
render() {
return ;
}
}
上述方法中的任何一种都可能解决自动填充不起作用的问题。根据你的具体情况,选择适合你的方法进行尝试。
上一篇:表单选项组件
下一篇:表单选择查询输出-数组