reactjs – 无法获取材料的属性-ui SelectField in react
发布时间:2020-12-15 09:34:58 所属栏目:百科 来源:网络整理
导读:我正在使用 SelectField材料-ui作为我的反应项目. 我从这个答案 Can’t get the target attributes of material-ui select react component 尝试了很多方法. 但它们不起作用.我的target.id总是等于“” 我怎样才能获得属性(如id). 这是我的代码: constructo
我正在使用
SelectField材料-ui作为我的反应项目.
我从这个答案 Can’t get the target attributes of material-ui select react component 尝试了很多方法. 但它们不起作用.我的target.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,value) => this.props.selectChange(e,value,this.props.cityName)} maxHeight={200} > {cities} </SelectField> ); } } 在你的主要功能中你会说(代码被切除一些部分省略) handleSelectChange(e,name){ this.setState({ [name] : value,}); } render(){ return ( <cityForm cityName = "city1" city={this.state.city1} selectChange={this.handleSelectChange}/> ); } } 我正在构建一个动态表单生成器,它为我做了诀窍=). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |