Javascript 如何在动态选择框中添加OnChange每个选项

Javascript 如何在动态选择框中添加OnChange每个选项,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,在我的项目中,在“选择”框中动态添加选项 ComponentDidMount(){ var status_id = document.getElementById('c_status').options; } Data.forEach( (option) => { status_id.add( new Option(option,option,true)), }): 常量数据=[“活动”,“暂停]//常数数组 render(){ 返回() } 我的问题是选择

在我的项目中,在“选择”框中动态添加选项

ComponentDidMount(){
   var status_id = document.getElementById('c_status').options;
 }
 Data.forEach( (option) => {
       status_id.add( new Option(option,option,true)),
  }):
常量数据=[“活动”,“暂停]//常数数组

render(){
返回()
}
我的问题是选择框单击以显示值活动暂停
如何使用此选项onChange

我不确定您想要实现什么,但是
选项
标记没有
onChange
事件。但是您可以在
select
标记上使用
onChange
事件

 class Example extends React.Component {
    constructor(props){
    super(props);
    this.state = {
        options: ['active', 'paused']
    }
  }
  onChange(e){
    console.log(this.state.options[e.target.selectedIndex])
  }
  render() {
    return <select onChange={this.onChange.bind(this)}>
      {this.state.options.map(el => <option key={el} value={el}>{el}</option>)}
    </select>;
  }
}
类示例扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
选项:[“活动”、“暂停”]
}
}
onChange(e){
console.log(this.state.options[e.target.selectedIndex])
}
render(){
返回
{this.state.options.map(el=>{el}}
;
}
}

阅读react文档的表单部分。甚至有一个onChange的例子
 class Example extends React.Component {
    constructor(props){
    super(props);
    this.state = {
        options: ['active', 'paused']
    }
  }
  onChange(e){
    console.log(this.state.options[e.target.selectedIndex])
  }
  render() {
    return <select onChange={this.onChange.bind(this)}>
      {this.state.options.map(el => <option key={el} value={el}>{el}</option>)}
    </select>;
  }
}