简体   繁体   English

如何从 reactJS 中的无线电表单中获取价值

[英]How to get value from radio form in reactJS

I Have this form in one of my react components我的一个反应组件中有这个表格

      <div className="form-check">
        <input
          type="radio"
          className="form-check-input"
          value={3}
          name="priority"
        />
        <label className="form-check-label">High Priority</label>
      </div>
      <div className="form-check">
        <input
          type="radio"
          value={2}
          className="form-check-input"
          name="priority"
        />
        <label className="form-check-label">Medium Priority</label>
      </div>
      <div className="form-check">
        <input
          type="radio"
          value={1}
          className="form-check-input"
          name="priority"
        />
        <label className="form-check-label">Low Priority</label>
      </div>

I then have an onclick function on a button.然后我在按钮上有一个 onclick function。 I want to get the value of the radio form in my function.我想在我的 function 中获取无线电表单的值。 How do I do that?我怎么做? Is there a way to do it with the useRef hook?有没有办法使用 useRef 挂钩? Note: I am using functional components so any solution with class components will not help.注意:我使用的是功能组件,因此任何使用 class 组件的解决方案都无济于事。

Thank you!谢谢!

Use state to maintain the value of the selected radio input.使用 state 保持所选无线电输入的值。

We add one listener to the parent element so we can use event delegation which calls a function when it captures a change event from a child input element.我们向父元素添加一个侦听器,因此我们可以使用事件委托,当它从子输入元素捕获更改事件时,它会调用 function。 When it's called it checks the clicked element is an input (radio) and then sets the state with its value.当它被调用时,它会检查单击的元素是一个输入(无线电),然后将 state 设置为其值。

The button simply logs the current state.该按钮仅记录当前的 state。

 const { useState } = React; function Example() { // Initialise state const [ radio, setRadio ] = useState(0); // When the button is clicked log the current state function handleClick(e) { console.log(radio); } // When an element is clicked function handleChange(e) { // Grab the nodeName and value from // the clicked element const { nodeName, value } = e.target; // Because we're using event delegation (attaching // an event listener to a parent element and capturing // events from child elements as they "bubble up" the DOM) // we need to check to see if the clicked element is an input if (nodeName === 'INPUT') { // Set the state with the input value setRadio(value); } } return ( <div onChange={handleChange}> <div className="form-check"> <input type="radio" className="form-check-input" value={3} name="priority" /> <label className="form-check-label">High Priority</label> </div> <div className="form-check"> <input type="radio" value={2} className="form-check-input" name="priority" /> <label className="form-check-label">Medium Priority</label> </div> <div className="form-check"> <input type="radio" value={1} className="form-check-input" name="priority" /> <label className="form-check-label">Low Priority</label> </div> <button onClick={handleClick}>Show radio state</button> </div> ); }; ReactDOM.render( <Example />, document.getElementById('react') );
 button { margin-top: 0.5em; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script> <div id="react"></div>

I have created demo here, you could add onChange method to each input and then save inside in a state.我在这里创建了演示,您可以为每个输入添加 onChange 方法,然后将其保存在 state 中。

Demo:https://codesandbox.io/s/tender-sinoussi-1t7fi?file=/src/App.js演示:https://codesandbox.io/s/tender-sinoussi-1t7fi?file=/src/App.js

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

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