简体   繁体   English

在反应中从虚拟键盘获取输入字段中的值

[英]get value in input field from virtual keyboard in react

Good Day, I am working on a countdown watch which have a virtual numpad (given in code as button) and three input (hour.minute and second), now i want that whenever i click input box and type in virtual numpad.it print the value in that box only.美好的一天,我正在开发一个倒计时手表,它有一个虚拟小键盘(在代码中作为按钮给出)和三个输入(小时.分钟和秒),现在我想要每当我点击输入框并输入虚拟小键盘.it 打印仅限该框中的值。

  1. there i used three ref to getElement of input and change set their value but dont know how to set for specific box我使用三个 ref 来获取输入元素并更改设置它们的值,但不知道如何为特定框设置
const inputOne = useRef(null);
const inputTwo = useRef(null);
const inputThree = useRef(null);

  const changeValue = (val) => {
    inputOne.current.value  = setTime({hour: updatedHour + val, minute: updatedMinute, second: updatedSecond}); 
    inputTwo.current.value = setTime({hour: updatedHour, minute: updatedMinute + val, second: updatedSecond});
    inputThree.current.value = setTime({hour: updatedHour, minute: updatedMinute, second: updatedSecond + val}); 
    }

const changeTime = (e) => {
    setTime({ ...time, [e.target.name]: e.target.value });
  };
  1. this is input fields i use, updatedHour,updatedMinute and updatedSecond is part of setTime state.这是我使用的输入字段,updatedHour、updatedMinute 和 updatedSecond 是 setTime state 的一部分。
  <input ref={props.inputOne} onChange={props.changeTime} name="hour"  placeholder="Hour"  value={props.updatedHour} />
          <input ref={props.inputTwo} onChange={props.changeTime} name="minute"  placeholder="Minute" value={props.updatedMinute} />
          <input ref={props.inputThree} onChange={props.changeTime} name="second"  placeholder="Second" value={props.updatedSecond} />
  1. this is buttons which create virtual keyboard这是创建虚拟键盘的按钮
 <button className="grid-item" tpye='button' onClick={()=> props.changeValue('1')}>1</button>
  <button className="grid-item" tpye='button' onClick={()=> props.changeValue('2')}>2</button>
  <button className="grid-item" tpye='button' onClick={()=> props.changeValue('3')}>3</button>  
  <button className="grid-item" tpye='button' onClick={()=> props.changeValue('4')}>4</button>
  <button className="grid-item" tpye='button' onClick={()=> props.changeValue('5')}>5</button>
  <button className="grid-item" tpye='button' onClick={()=> props.changeValue('6')}>6</button>  
  <button className="grid-item" tpye='button' onClick={()=> props.changeValue('7')}>7</button>
  <button className="grid-item" tpye='button' onClick={()=> props.changeValue('8')}>8</button>
  <button className="grid-item" tpye='button' onClick={()=> props.changeValue('9')}>9</button>  
  <button className="grid-item" tpye='button' >Prev</button>  
  <button className="grid-item" tpye='button' onClick={()=> props.changeValue('0')}>0</button>  
  <button className="grid-item" tpye='button' >Next</button>  

I've just copied whatever you've been writing and added it to stackblitz , and I've added some changes to it the link is below, hope you find it useful:我刚刚复制了您一直在写的任何内容并将其添加到stackblitz中,并且我已经对其进行了一些更改,链接如下,希望您觉得它有用:

link to stackblitz for the solution链接到 stackblitz 以获得解决方案

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

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