当前位置 : 主页 > 网页制作 > React >

reactjs – 无法获取材料的属性-ui SelectField in react

来源:互联网 收集:自由互联 发布时间:2021-06-15
我正在使用 SelectField材料-ui作为我的反应项目. 我从这个答案 Can’t get the target attributes of material-ui select react component 尝试了很多方法. 但它们不起作用.我的target.id总是等于“” 我怎样才
我正在使用 SelectField材料-ui作为我的反应项目.
我从这个答案 Can’t get the target attributes of material-ui select react component
尝试了很多方法.

但它们不起作用.我的target.id总是等于“”
我怎样才能获得属性(如id).

这是我的代码:

constructor(props) {
    super(props);
    this.state = {
        form: {
            resident_city: ''

        },
        ret_code: '',
        ret_msg: ''
    };
    this.handleList = this.handleList.bind(this);
}

handleList(event, index, value) {
    event.persist()
    const field = event.target.id;
    const form = this.state.form;
    form[field] = value;
    console.log(event, value)
    this.setState({
        form
    });
    console.log(form);
}


        <form>
            <SelectField
                style={style}
                id="city"
                value={this.state.form.resident_city}
                onChange={this.handleList}
                maxHeight={200}
            >
                {cities}
            </SelectField>
        </form>

更新

我尝试使用没有表单的SelectField,我仍然无法获得id属性.这真让我感到困惑.

在主要组件上,您为选择表单组件定义一个prop名称,假设您的城市组件被调用:cityForm

在您的cityForm组件中

render() { 
return (
       <SelectField
                style={style}
                value={this.props.city}
                onChange={(e, index, value) => this.props.selectChange(e, index, value, this.props.cityName)}
                maxHeight={200}
            >
                {cities}
            </SelectField>
);
}
}

在你的主要功能中你会说(代码被切除一些部分省略)

handleSelectChange(e, index, value, name){
      this.setState({
      [name] : value,
    });
  }

render(){ 
 return (
  <cityForm cityName = "city1"  city={this.state.city1} selectChange={this.handleSelectChange}/>
  );
 }
}

我正在构建一个动态表单生成器,它为我做了诀窍=).

网友评论