I want the users to select items form checkbox and submit a numerical price according to the items selected. I have tried it here using if statement. How to do this efficiently?? i had tried to map the states using this.state.map() but its not working also.
constructor(props) {
super();
this.state = {
check1: false,
check2: false,
check3: false,
check4: false,
};
this.onCheckChange = this.onCheckChange.bind(this);
this.onSubmit = this.onSubmit.bind(this);
}
onCheckChange(e) {
this.setState({
[e.target.name]: e.target.checked,
});
}
//**************i want to change this ****************
onSubmit(e) {
e.preventDefault();
var price = 0;
if (this.state.check1 === true) {
price = price + 10;
}
if (this.state.check2 === true) {
price = price + 20;
}
if (this.state.check3 === true) {
price = price + 30;
}
if (this.state.check4 === true) {
price = price + 40;
}
console.log(price);
}
render() {
return (
<div>
<Form onSubmit={onSubmit}>
checkboxes are placed
</form>
</div>
);
}
}
export default SellComponent;
Aucun commentaire:
Enregistrer un commentaire