简体   繁体   English

如何在选择单选按钮时启用/禁用按钮?

[英]How to enable/disable button on selecting radio button?

I am new to reactjs. 我是Reactjs的新手。 Currently in my application, I want to enable and disable the button on selecting radio button. 当前在我的应用程序中,我想启用和禁用选择单选按钮上的按钮。 Below is my code : 下面是我的代码:

class Radiosample extends React.Component {

   render() {
      <table>
       <tbody>
         <tr>
           <td>
              <Field name="radio1" component="input" id="radio1" type="radio"/> // On check of this radio button below button should enable.. 
           </td>
           <Button name="button" id="btn">Click Me</Button>
         </tr>
       </tbody>
      </table>
   }
}

export default Radiosample

Thanks 谢谢

You should use state , otherwise the page would not be rerendered. 您应该使用state ,否则页面将不会重新呈现。

So onClick or onChange event you need to update state 所以onClickonChange事件需要更新状态

setButtonDisability = (event: React.MouseEvent<HTMLInputElement>) => this.setState({ isButtonDisabled: /* value from event target */ })

Just add to your <Field /> component onClick={this.setButtonDisability} or onChange={this.setButtonDisability} . 只需将onClick={this.setButtonDisability}onChange={this.setButtonDisability}添加到您的<Field />组件中。

And then use it in render function 然后在渲染函数中使用它

<Button name="button" disabled={this.state.isButtonDisabled} />

You should definitly go through an oficial intro tutorial . 您应该明确地通过官方入门教程

Not very generic, but shouldn't this simple thing work? 不是很通用,但是这个简单的东西不行吗?

class Radiosample extends React.Component {
    function disableButton() {
        document.getElementById("btn").disabled = true;
    } 

    render() {
      <table>
       <tbody>
         <tr>
           <td>
              <Field name="radio1" onclick="this.disableButton()" component="input" id="radio1" type="radio"/> // On check of this radio button below button should enable.. 
           </td>
           <Button name="button" id="btn">Click Me</Button>
         </tr>
       </tbody>
      </table>
   }
}

export default Radiosample

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

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