嘿,各位新人做出反应,试图实现一些简单的验证,但努力奋斗。我想
我想在飞行中做所有这些,但一直在努力/无法找到任何可靠的参考。这是我的代码
import React from 'react';
//import ReactDOM from 'react-dom';
import { Button, Form, FormGroup, Label, Input } from 'reactstrap';
export default class Forms extends React.Component {
constructor() {
super();
this.state = {
fname: '',
lname: '',
email: '',
password: '',
confirmPassword: '',
formSuccess: false
}
this.handleSubmit = this.handleSubmit.bind(this);
}
handleUserInput(e) {
const name = e.target.name;
const value = e.target.value;
this.setState({ [name]: value });
}
handleSubmit(e) {
e.preventDefault();
}
render() {
return (
<Form onSubmit={this.handleSubmit}>
<FormGroup>
<Label for="fname">First Name</Label>
<Input
value={this.state.fname}
onChange={(event) => this.handleUserInput(event)}
type="text"
name="fname"
id="fname"
placeholder="first name"
/>
</FormGroup>
<FormGroup>
<Label for="lname">Last Name</Label>
<Input
onChange={(event) => this.handleUserInput(event)}
value={this.state.lname}
type="text"
name="lname"
id="lname"
placeholder="last name"
/>
</FormGroup>
<FormGroup>
<Label for="email">Email</Label>
<Input
onChange={(event) => this.handleUserInput(event)}
value={this.state.email}
type="email"
name="email"
id="email"
placeholder="email"
/>
</FormGroup>
<FormGroup>
<Label for="password">Password</Label>
<Input
onChange={(event) => this.handleUserInput(event)}
type="password"
value={this.state.password}
name="password"
id="password"
placeholder="password"
/>
</FormGroup>
<FormGroup>
<Label for="confirmPassword">Confirm Password</Label>
<Input
onChange={(event) => this.handleUserInput(event)}
value={this.state.confirmPassword}
type="password"
name="confirmPassword"
id="confirmPassword"
placeholder="confirm password"
/>
</FormGroup>
<FormGroup check>
<Label check>
<Input type="checkbox" />{' '}
Check me out
</Label>
</FormGroup>
<Button color="primary" size="lg" disabled={!this.state.formSuccess}>Submit</Button>
</Form>
)
}
}
感谢所有帮助
答案 0 :(得分:1)
为您的组件添加一个函数,如下所示:
<Button color="primary" size="lg" disabled={!this.isFormValid}>Submit</Button>
然后将您的按钮更改为:
formSuccess
这样你甚至不需要你所在州的Url.RouteUrl
。
答案 1 :(得分:0)
您应该有一个名为isAvailable的状态或类似的东西,您应该在按钮中使用它,如下所示:
`<Button color="primary" size="lg" disabled={!this.state.isAvailable}>Submit</Button>`
然后,在函数handleUserInput中,您应该更新该状态:
`handleUserInput(e) {
const name = e.target.name;
const value = e.target.value;
this.setState({ name: value, isAvailable: true });
}`
答案 2 :(得分:0)
您可以使用react-validation 如果所有验证都为真,则启用按钮
答案 3 :(得分:0)
Working codepen **https://codesandbox.io/s/yjn2zz4qvj**
导入React,{Component}来自&#34;反应&#34 ;;
export default class Hello extends Component {
state = {
email: "",
name: ""
};
handleChange = e => {
const { value, name } = e.target;
this.setState({ [name]: value });
};
render() {
return (
<div>
<form className="col-md-5">
<div className="form-group">
<label> Email: </label>
<input
name="email"
value={this.state.email}
placeholder="email"
onChange={this.handleChange}
/>{" "}
<br />
</div>
<div className="form-group">
<label> Name: </label>
<input
name="name"
value={this.state.name}
placeholder="name"
onChange={this.handleChange}
/>
</div>
<button
type="button"
disabled={!this.state.email || !this.state.name}
>
Button
</button>
</form>
</div>
);
}
}