[英]How do I clear the input field after an incorrect entry
I have an input form name password below, and I would like to clear it after it submits and there is an incorrect password. 我在下面有一个输入表单名称密码,我想在提交后清除它,并且密码不正确。 I am trying e.target.password.value = '' below also tried e.target.password.value.reset() but I get an error that says that reset is not a function. 我正在尝试e.target.password.value ='',下面也尝试了e.target.password.value.reset(),但是我收到一条错误消息,说reset不是函数。
onPasswordSubmit = (e) => {
axios.get('/api/password/').then(response => {
const password = response.data[0].password;
((password === this.state.password) ? this.setState(() => ({
passwordVerified: true,
error: ''
})) : this.setState(() => ({
error: 'Incorrect Password',
})));
(this.state.passwordVerified) ? (this.props.changeState()) :
null
})
}
render() {
return (
<div>
<div className="row">
<div className="input-field col s12">
<form
onSubmit={(e) => {
e.preventDefault()
this.onPasswordSubmit(e);
e.target.password.value = ''
}}>
<input
id="password"
name="password"
type="password"
autoFocus
value={this.state.password}
onChange={this.onPasswordChange}
/>
<label htmlFor="password" className='active'>Password</label>
<br></br>
<br></br>
<br></br>
<Button buttonLabel='Enter'/>
</form>
</div>
</div>
</div>
As your input has a value from state
so you need to use setState
to clear the password state 由于您的输入具有来自state
的值,因此您需要使用setState
清除密码状态
Clear the state after request successfully done 成功完成请求后清除状态
onPasswordSubmit = (e) => {
axios.get('/api/password/').then(response => {
const password = response.data[0].password;
((password === this.state.password) ? this.setState(() => ({
passwordVerified: true,
error: '',
password: '', //if success
})) : this.setState(() => ({
error: 'Incorrect Password',
password: '', //if error
})));
(this.state.passwordVerified) ? (this.props.changeState()) :
null
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.