如果用户不在 ReactJs 中触摸它,我想存储选择的默认值。这怎么可能?
<select onChange={this.valSelected.bind(this)}>
{currencies.map(function(name, index){
return <option value={name}>{name}</option>;
})}
</select>
and
valSelected(event){
this.setState({
valSelected: event.target.value
});
}
您只需向 select 元素添加一个 value 属性,该属性由您所在的州设置。
<select value={this.state.valSelected} onChange={this.valSelected.bind(this)}>
{currencies.map(function(name, index){
return <option value={name}>{name}</option>;
})}
</select>
反应文档中对此进行了描述:Doc Link https://facebook.github.io/react/docs/forms.html#the-select-tag
然后在构造函数中或使用 getInitialState 为组件设置默认状态:在 React / React Native 中使用构造函数与 getInitialState 有什么区别? https://stackoverflow.com/questions/30668326/what-is-the-difference-between-using-constructor-vs-getinitialstate-in-react-r
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)