简体   繁体   中英

Get radio value on change - Reactjs

I am trying to get radio's value onChange but it's giving me error:

Uncaught TypeError: Cannot read property 'target' of undefined

Here is the code:

<input className="tab-input" value="a" id="tab1" type="radio" name="tabs" onChange={() => this.handleTabChange()} />
<label className="tab-label" htmlFor="tab1">Codepen</label>

<input className="tab-input" value="b" id="tab2" type="radio" name="tabs" onChange={() => this.handleTabChange()} />
<label className="tab-label" htmlFor="tab2">Dribbble</label>

<input className="tab-input" value="c" id="tab3" type="radio" name="tabs" onChange={() => this.handleTabChange()} />
<label className="tab-label" htmlFor="tab3">Dropbox</label>

Here is handleTabChange Code:

handleTabChange(event){
    console.log(event.target.value);
  }

I have bind the function in constructor:

this.handleTabChange = this.handleTabChange.bind(this);

Don't know what is wrong.

You are expecting event as a parameter, but you are not passing it.

This should solve it

<input className="tab-input" value="a" id="tab1" type="radio" name="tabs" onChange={(e) => this.handleTabChange(e)} />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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