reactjs - 按下按钮后无法清除输入字段
问题描述
class CategoryBox extends Component {
constructor(props) {
super(props);
this.state = {
newCategoryTitle: '',
errorMsg: null,
updateCategoryTitle: '',
};
this.handleCreateCategory = this.handleCreateCategory.bind(this);
this.handleUpdateCategory = this.handleUpdateCategory.bind(this);
}
...
...
handleUpdateCategory(e) {
e.preventDefault();
this.setState({
updateCategoryTitle: ''
});
}
render() {
return (
//...
<form>
<input
type={'text'}
className={styles.tabSmallField}
placeholder={ category.name }
onChange={(e) => { this.setState({ updateCategoryTitle: e.target.value }); }} />
<button type="submit" onClick={this.handleUpdateCategory}>Update</button>
</form>
//...
}
}
我想在按下按钮后清除输入文本字段。
看起来它在按下按钮后进入“handleUpdateCategory(e)”。但它不会清除按钮上方的输入字段。
如何清除输入文本字段?
解决方案
答案在评论中。
It didn't clear because you never bind the state value to your <input>. Change it like this <input value={this.state.updateCategoryTitle} ...other props... />
推荐阅读
- javascript - 如何像mysql一样从jQuery中的json或数组数据中搜索名称
- python-3.x - 创建默认值
- javascript - RxJS - 检测长鼠标按下
- c# - json 用 newtonsoft 序列化列表
- javascript - 如何在 eval 语句中插入括号以对 JavaScript 中的操作进行排序?
- javascript - 如何将字符串数组转换为二维数组?
- linux-kernel - 与内核和编译器优化共享内存
- jquery - jQuery验证自定义函数 - 超过最小值
- android - 活动可见时立即显示 toast
- spring-boot - 如何将代码从 Spring Security 迁移到 Reactive Spring Security?