【问题标题】:Unable to type password in input box无法在输入框中输入密码
【发布时间】:2025-12-16 05:15:01
【问题描述】:

我正在创建一个简单的登录页面。当我声明状态并尝试在输入字段的值中使用它们时,我无法在输入框中输入或键入任何内容。

这个问题只出现在密码输入框,邮箱输入框正常。

export default class SignIn extends Component{
    constructor(props){
        super(props)
        this.state = {
            email:'',
            password:''
        };
        this.handleEmailChange = this.handleEmailChange.bind(this);
        this.handlePasswordChange = this.handlePasswordChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
        };
        handleEmailChange(event) {
            this.setState({email: event.target.value});
            console.log(this.state.email)
        }
        handlePasswordChange(event) {
            this.setState({Password: event.target.value});
            console.log(this.state.password)
        }
        handleSubmit(event) {
            event.preventDefault();
            const fdata = new FormData()
            fdata.append('email',this.state.email)
            fdata.append('password',this.state.password)
            axios({
                method: 'post',
                url: 'abc.com/abc',
                data: fdata
            })
            .then(function(response){
                console.log(response)
                response.json()
            })
            .then(function(responseJson){
                console.log(responseJson)
            })
            .catch(function(){
                alert("Failed")
            })
        }
    render(){
        return(
            <div>
                <div className="container mt-5">
                <form action="/action_page.php">
                    <div className="form-group">
                    <label htmlFor="email">Email:</label>
                    <input  type="email"
                            value={this.state.email}
                            onChange={this.handleEmailChange}
                            className="form-control" 
                            id="email"
                            placeholder="Enter email" 
                            name="email" />
                    </div>
                    <div className="form-group">
                    <label htmlFor="pwd">Password:</label>
                    <input  type="password"
                            value={this.state.password}
                            onChange={this.handlePasswordChange}
                            className="form-control" 
                            id="pwd" 
                            placeholder="Enter password" 
                            name="pswd" />
                    </div>
<button type="submit" onClick={this.handleSubmit}>Submit</button>
                </form>
                </div>
            </div>
        )
    }
}

我希望在输入类型密码中使用密码状态,并且不想使用任何默认值。

【问题讨论】:

    标签: javascript reactjs jsx


    【解决方案1】:

    将密码更改为setState中的密码

    你的代码

    handlePasswordChange(event) {
                    this.setState({Password: event.target.value});
                    console.log(this.state.password)
                }
    
    

    新代码

    handlePasswordChange(event) {
                    this.setState({password: event.target.value});
                    console.log(this.state.password)
                }
    

    【讨论】:

      【解决方案2】:

      你的函数中有错字:

      handlePasswordChange(event) {
          this.setState({Password: event.target.value}); 
          // should be 
          this.setState({password: event.target.value}); 
          console.log(this.state.
      }
      

      【讨论】: