简体   繁体   English

将 TextField 值存储在 MaterialUI/React 的变量中

[英]Store a TextField value inside a variable in MaterialUI/React

I have a TextField and would like to take the input and store it in a variable.我有一个 TextField 并且想获取输入并将其存储在一个变量中。

<TextField
  id="output_elevator-amount"
  label="Elevator amount"
  color="primary"
  // variant="outlined"
  type="number"
  InputLabelProps={{
    shrink: true
  }}
  InputProps={{
    readOnly: true
  }}
  onChange={(event) => {
    setAmount(parseInt(event.target.value));
  }}
/>

When I was working with javascript and html I did something like this but now with typescript/MUI/React I'm not sure what to do.当我使用 javascript 和 html 时,我做了类似的事情,但现在使用 typescript/MUI/React 我不知道该怎么做。

let elevatorAmt = (document.getElementById('output_elevator-amount') as HTMLInputElement);

any help is greatly appreciated.任何帮助是极大的赞赏。

If you want to store "somewhere" the variable inputed via onChange, you are already doing by setting setAmount .如果您想将通过 onChange 输入的变量“存储在某处”,您已经通过设置setAmount

I am assuming you are using this [amount, setAmount] = useState('') .我假设您正在使用此[amount, setAmount] = useState('') Try console logging amount and use it where you need.尝试控制台日志记录amount并在需要的地方使用它。

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

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