react下拉框获取值
发布时间:2020-12-15 16:14:59 所属栏目:百科 来源:网络整理
导读:1.遍历选项,option里的value去掉,把value写到select里 2.onChange方法写到select标签里 import React,{ Component } from ‘react‘ class App extends Component { constructor(props) { super(props); this .state = { cities:[ "北京","上海","广州"],
|
1.遍历选项,option里的value去掉,把value写到select里 2.onChange方法写到select标签里 import React,{ Component } from ‘react‘
class App extends Component {
constructor(props) {
super(props);
this.state = {
cities:["北京","上海","广州"],//选项
city:"广州" // 默认选项
}
}
render() {
return (
<div>
{/* value放在select里 select框也要onChcange*/}
<select value={this.state.city} onChange={(e)=>this.getValue(e)}>
{
// 遍历option
this.state.cities.map((ele,index)=>{
return(
<option key={index}>{ele}</option>
)
})
}
</select>
</div>
);
}
getValue=(event)=>{
//获取被选中的值
console.log(event.target.value);
this.setState({
//默认值改变
city:event.target.value
})
}
}
export default App;
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
