javascript - 无法在输入框中输入密码

标签 javascript reactjs jsx

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

只有密码输入框才会出现此问题,电子邮件输入框工作正常。

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>
        )
    }
}

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

最佳答案

将密码更改为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)
            }

关于javascript - 无法在输入框中输入密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56040015/

相关文章:

javascript - 映射数组到对象

javascript - AWS Dynamodb 查询 - 从有条件的表中获取项目

javascript - 在reactJS中动态创建的数组长度

javascript - React 返回要渲染的组件数组未正确嵌套

javascript - href 代替 javascript 工作

javascript - 单击按钮在 GoogleMap 中移动

javascript React Form onsubmit 保存提交的用户名

javascript - 如何使用 react Hook 将 HTML 复制到剪贴板?

ruby-on-rails - 来自 react 应用程序的带有 JSON 有效负载的 HTTP POST 请求返回 net::ERR_SSL_PROTOCOL_ERROR(chrome)

javascript - 在占位符 JSX 中渲染特殊字符