2017-06-05 11 views
1

selectの関数にオプションの値を渡す方法は?反応の関数にオプションの値を渡す方法は?

submit = (selectedOption) => { 
    console.log('Selected value:',selectedOption); 
} 

render(){ 
    return 
     <select onChange={this.submit}> 
      <option value="Bangalore">Bangalore</option> 
      <option value="Delhi">Delhi</option> 
      <option value="Mumbai">Mumbai</option> 
     </select> 
} 

答えて

2

手動で値を渡す必要はありません、あなたはevent objectで選択された値にアクセスすることができます。

submit = (event) => { 
    console.log('Selected value:', event.target.value); 
} 

render(){ 
    return(
     <select onChange={this.submit}> 
     <option value="Bangalore">Bangalore</option> 
     <option value="Delhi">Delhi</option> 
     <option value="Mumbai">Mumbai</option> 
    </select> 
    ) 
} 
event.target.value

はこのようにそれを書きます

0

onChangeのコールバックは、イベントオブジェクトをパラメータとして受け取ります。選択した値にアクセスするには、event.target.valueを使用します。例えば。

<select onChange={(event) => this.submit(event.target.value)}> 
関連する問題