How can I send the value of the selected option to a function?

0

Good afternoon, now what I'm trying to do is the following:

I have a <select> that has multiple categories with an id each, what I want to do is that in the case that this select is changed, I mean in a onChange , it was sent as an argument to a function x, the value of this option selected, or the id if it had to be done in another way.

My select

<select>
        <option default> ---- Select Category ---- </option>

        {this.state.items.length ?
        this.state.items.map(item=>

        <option value={item.id}>{item.name}</option>

            ) 
        : <li>Loading...</li>
      }
    </select>
    
asked by Santiago D'Antuoni 20.01.2017 в 19:21
source

2 answers

2

I found the solution,

<select onChange={this.selectKey.bind(this)}>

selectKey(event) {  
  this.setState({ exampleState: event.target.value });
}
    
answered by 20.01.2017 в 19:46
0

A simpler solution, could be the following

<select onChange="alert(this.value)">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
    
answered by 20.01.2017 в 19:54