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>;
}
}