如何获取相应按钮的值属性?

时间:2017-03-20 20:53:23

标签: javascript reactjs

buttonPress = (event) => { console.log(event.props.value)}

  <RaisedButton ref={(button) => { this.RaisedButton = button; }} label="Primary" value = "year" onTouchTap={()=>this.buttonPress( this.RaisedButton )}  primary={true} />

当我尝试渲染上面的一个按钮时,我得到了值。但是,当我渲染多个按钮时,我只获得所有按钮的最后一个按钮的值。无论我点击什么按钮,我都会year

    buttonPress = (event) => { console.log(event.props.value)}

      <RaisedButton ref={(button) => { this.RaisedButton = button; }} label="Primary" value = "day" onTouchTap={()=>this.buttonPress( this.RaisedButton )}  primary={true} />
 <RaisedButton ref={(button) => { this.RaisedButton = button; }} label="Primary" value = "month" onTouchTap={()=>this.buttonPress( this.RaisedButton )}  primary={true} />
 <RaisedButton ref={(button) => { this.RaisedButton = button; }} label="Primary" value = "year" onTouchTap={()=>this.buttonPress( this.RaisedButton )}  primary={true} />

如何获取相应按钮的值?

1 个答案:

答案 0 :(得分:2)

您只对一个按钮保留一个引用,并且引用回调相互覆盖。相反,您可以将它们分成三个单独的引用

<RaisedButton ref={(button) => { this.RaisedButtonDay = button; }} label="Primary" value = "day" onTouchTap={()=>this.buttonPress( this.RaisedButtonDay )}  primary={true} />
<RaisedButton ref={(button) => { this.RaisedButtonMonth = button; }} label="Primary" value = "month" onTouchTap={()=>this.buttonPress( this.RaisedButtonMonth )}  primary={true} />
<RaisedButton ref={(button) => { this.RaisedButtonYear = button; }} label="Primary" value = "year" onTouchTap={()=>this.buttonPress( this.RaisedButtonYear )}  primary={true} />

在旁注中,您传入buttonPress的内容不是事件对象,而是对按钮本身的引用。