问题
I have this function
handleChangeButton = (e) => {
alert(e.target.value)
this.props.setFieldValue('degreeLevel', e.target.value);
}
and in my component render, I have
<div className="twelve columns">
<p>Degree Level</p>
<Button
variant="raised"
label="Default"
onClick = { this.handleChangeButton }
value="Doctorate"
>
Doctorate
</Button>
<Button variant="raised" label="Default">
Masters
</Button>
<Button variant="raised" label="Default">
Undergraduate
</Button>
</div>
So, what I want to do is, when I click the Doctorate
button, it should this.props.setFieldValue
to degreeLevel
which is one of the fields in my Formik
form. When I click the button, the alert gives me undefined
which means it's not reading the value Doctorate
.
How can I make e.target.value
read the value of the button?
回答1:
Use currentTarget
instead of target
handleChangeButton = (e) => {
alert(e.currentTarget.value)
this.props.setFieldValue('degreeLevel', e.currentTarget.value);
}
来源:https://stackoverflow.com/questions/50414059/getting-values-from-material-ui-button-in-react