我将如何动态创建一个多级下拉菜单并获取所选的子菜单。
答案 0 :(得分:1)
您可以通过event.target.value
并使用选定的class App extends React.Component {
state = {
selectedOption: ""
};
selectHandler = event => {
console.log(event.target.value);
this.setState({ selectedOption: event.target.value });
};
render() {
let data = ["first", "second", "third", "fourth"];
return (
<select
onChange={(event) => this.selectHandler(event)}
value={this.state.selectedOption}
>
{data.map(value => (
<option value={value} key={value}>
{value}
</option>
))}
</select>
);
}
}
ReactDOM.render(<App/>, document.getElementById('root'));
对其进行更新。这是可行的解决方案。
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id='root' />
{{1}}