简体   繁体   English

如果我使用的是 Material Ui,我如何在 React 中获取输入字段的值?

[英]How do I get value of input field in React, if I am using Material Ui?

I have few radio inputs which are imported from material Ui react components,我几乎没有从材料 Ui 反应组件中导入的无线电输入,

      <FormControlLabel onClick={checkAnswerHandler} value={answer} control={<Radio color="primary" />} label={<span dangerouslySetInnerHTML={{ __html: answer}}></span>} />
</div>

Here, I tried to access the value of input through onClick function like below在这里,我尝试通过 onClick function 访问输入值,如下所示

const checkAnswer = (e: React.MouseEvent<HTMLLabelElement>) => {
 
     answer = e.currentTarget.value; //but throws error as label is not an input element
     
  }

so, How do I get the Value of input tag cause I am unable to implement onClick function to input tag?那么,如何获取输入标签的值,因为我无法将onClick function实施到输入标签?

You should get the value from RadioGroup您应该从RadioGroup获取值

look at this example from Matrial-ui documentation:请看 Matrial-ui 文档中的这个例子:

 <FormControl component="fieldset"> <FormLabel component="legend">Gender</FormLabel> <RadioGroup aria-label="gender" name="gender1" value={value} onChange={handleChange}> <FormControlLabel value="female" control={<Radio />} label="Female" /> <FormControlLabel value="male" control={<Radio />} label="Male" /> <FormControlLabel value="other" control={<Radio />} label="Other" /> <FormControlLabel value="disabled" disabled control={<Radio />} label="(Disabled option)" /> </RadioGroup> </FormControl>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM