2017-10-10 127 views
0

onClick ={props.onPassFail}移动到包装<Label>时触发,但在连接到<Input>控件时不触发。我最终想要触发onChange事件,并不明白为什么Input没有注册onClick或onChange事件。我遵循Bootstrap 4.0.0-beta要求的单选按钮约定。onClick不触发React.js输入单选按钮

如何将onClick和onChange直接连接到<Input>控件?

代码:

<div className="btn-group" data-toggle="buttons"> 
    <label className="btn btn-secondary passfail passfail-fail" onClick={props.onPassFail}> 
    <input type="radio" name="passfail" id="option1" autoComplete="off" /> Fail 
    </label> 
    <label className="btn btn-secondary passfail passfail-pass" onClick={props.onPassFail}> 
    <input type="radio" name="passfail" id="option2" autoComplete="off" /> Pass 
    </label> 
</div> 
+0

你有plunkr为了这 ? –

+0

传递onClick输入,而不是标签 – Constantine

+0

错误是传递onClick到输入什么都不做。 @Amt Labib的解决方案是使用React组件而不是无状态函数。 – sammarcow

回答

0

您可以添加onClickonChange直接输入控制检查下面的代码,它的完全工作

onClick的处理器和的onChange是在组件和内这些附加功能功能可以调用任何处理程序通过props

export default class Test extends Component { 
    constructor(props) { 
     super(props); 
     this.clicked = this.clicked.bind(this); 
     this.changed = this.changed.bind(this); 
    } 

    clicked(){ 
     console.log("clicked"); 
     //this.props.onPassFail(); 
    } 

    changed(){ 
     console.log("changed"); 
     //this.props.onPassFail(); 
    } 

    render() { 
     const { handleSubmit , error } = this.props 
     return (
      <label> 
       <input type="radio" onClick={this.clicked} onChange={this.changed} /> 
      </label> 
     ); 
    } 
} 
+0

我的原始问题并不清楚,我正在尝试在无状态函数中执行此操作,而不是反应组件。那可能吗? – sammarcow