我想在我的表單中創建密碼確認功能。然而,在測試時,我的console.logs
輸出值總是一個值短,我不知道爲什麼會發生這種情況。onChange功能console.log是一個字符短
class SignUp extends React.Component {
getInitialState() {
return {
initialPassword: "",
confirmPassword: "",
isMatchedPassword: true,
isEmailInUse: false,
}
}
checkAvailableEmail(event) {
console.log("checkAvailableEmail");
console.log(event.target.value);
}
confirmPassword(event) {
console.log("confirmPassword");
this.setState({
confirmPassword: event.target.value
})
if (this.state.confirmPassword !== this.state.initialPassword) {
console.log("Passwords DONT match");
this.state.isMatchedPassword = false;
} else {
console.log("Passwords match");
this.state.isMatchedPassword = true;
}
console.log("confirmPassword props");
console.log(this.state);
}
setInitialPassword(event) {
console.log("setInitialPassword");
this.setState({
initialPassword: event.target.value
});
console.log("setInitialPassword props");
console.log(this.state);
}
render() {
let hidden = "";
Log.debug('Signup component props');
Log.debug(this.props);
return (
<div>
<form method="POST" action="/signup">
<ul>
<li><input id="firstName" name="first_name" placeholder="First name" type="text" required="required" /></li>
<li><input id="lastName" name="last_name" placeholder="Last name" type="text" required="required" /></li>
<li><input id="email" name="email" placeholder="Email" type="email" required="required" onBlur={ this.checkAvailableEmail }/></li>
<li><input id="password" name="password" placeholder="Password" type="password" required="required" onChange={ this.setInitialPassword}/></li>
<li><input id="confirmPassword" name="confirm_password" placeholder="Confirm Password" type="password" required="required" onChange={ this.confirmPassword } /></li>
<span id="confirmMessage" className={ hidden }>Password does not match</span>
<input type="submit" value="Sign up" />
<input type="reset" value="Cancel" />
</ul>
</form>
</div>
);
}
};