Answer the question
In order to leave comments, you need to log in
How to validate checkbox in ReactJS form?
The task is to make the submit button inactive if the agree checkbox is not pressed.
But I don't understand how to get the value from the checkbox...
var MyContactForm = React.createClass({
getInitialState() {
return {
disabled: true
};
},
resetValidation() {
this.setState({
disabled: true
});
},
validationState() {
var checked = this.refs.input.checked(); //Пытаюсь проверить таким образом, но безуспешно, документации не обнаружил
},
handleChange() {
this.setState( this.validationState() );
},
render: function() {
return (
<form>
<Input type="text" label="Name" placeholder="Your Name" bsSize="large" />
<Input type="email" label="Email" placeholder="Your Email" bsSize="large"/>
<Input type="textarea" label="Massage" placeholder="Please tell me more" bsSize="large"/>
<RulesAndConditions>Rules and Conditions</RulesAndConditions>
<Input type="checkbox" label="Confirm" readOnly onChange={this.handleChange()} bsSize="large"/>
<ButtonGroup vertical block>
<Button onClick={this.resetValidation} type="reset" bsStyle="warning" bsSize="large">Reset</Button>
<p/>
<Button type="submit" bsStyle="success" bsSize="large" disabled={this.state.disabled}>Submit</Button>
<p/>
<Button onClick={this.props.onHide} bsStyle="danger" bsSize="large">Close</Button>
</ButtonGroup>
</form>
)
}
});
Answer the question
In order to leave comments, you need to log in
I tried to check in this way, but to no avail, I did not find any documentation
<input ref="myCheckbox" ...
this.refs.myCheckbox.checked
handleChange(event) {
var checked = event.target.checked;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question