首页 > 解决方案 > 无法在输入字段中输入

问题描述

我有以下代码:

    constructor(props) {
        super(props);
        this.state = {
            search: "",
            value: "",
            username:'',
            email:'',
            password: '',
        };

        this.onChange = this.onChange.bind(this);
        this.onSubmit = this.onSubmit.bind(this);
    }

...
...

    onChange(e) {
        this.setState(
            {[e.target.name]: e.target.value})
    }

...
...

<input type="password" placeholder="Password"
onChange={e => this.onChange(e)}
value={ this.state.password }
                                                />

但我无法输入密码字段。如果我删除该value={ this.state.password }部分,然后我可以在该字段中输入,但是当该字段更改时,我的状态似乎没有更新。

问题是什么?

标签: reactjs

解决方案


您忘记为密码字段提供名称。它应该是

<input 
 type="password" 
 placeholder="Password"
 name="password"
 onChange={e => this.onChange(e)}
 value={ this.state.password }
/>

推荐阅读