javascript - select onchange event.value is undefined - Stack Overflow

In my ponent I want to do some stuff after the select value has changed.select:<select value={curren

In my ponent I want to do some stuff after the select value has changed.

select:

<select value={current} onChange={this.onSelectChange.bind(this)}>
    {options}
</select>

onchange event:

onSelectChange(event) {
    const { cookies } = this.props;

    // event.value is undefinded
}

I need the props in the event so I use bind(this). But why is event.value undefined (event.target.value also)

In my ponent I want to do some stuff after the select value has changed.

select:

<select value={current} onChange={this.onSelectChange.bind(this)}>
    {options}
</select>

onchange event:

onSelectChange(event) {
    const { cookies } = this.props;

    // event.value is undefinded
}

I need the props in the event so I use bind(this). But why is event.value undefined (event.target.value also)

Share asked May 12, 2017 at 5:19 DavidDavid 4,11711 gold badges52 silver badges105 bronze badges 1
  • I think there is an issue with some other piece of code in the ponent that uses this select element. If you can put the ponent code. Otherwise this code is all good and should work. – Hannan Commented May 12, 2017 at 5:39
Add a ment  | 

2 Answers 2

Reset to default 3

I think you are doing some other mistake, check the console.

Check this example event.target.value will always have the selected value:

class App extends React.Component{

  onSelectChange(e){
     console.log(e.target.value);
  }

  render(){
     return(
       <select onChange={this.onSelectChange.bind(this)}>
         <option value='1'>1</option>
         <option value='2'>2</option>
         <option value='3'>3</option>
       </select>
     )  
  }
}

ReactDOM.render(<App/>, document.getElementById('app'))
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id='app'/>

You can use also use this method.

<select value={current} onChange={()=>this.onSelectChange(e)}>
    {options}
</select>.

onSelectChange(e) {
  console.log(e.target.value)
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745071609a4609582.html

相关推荐

  • javascript - select onchange event.value is undefined - Stack Overflow

    In my ponent I want to do some stuff after the select value has changed.select:<select value={curren

    16小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信