在React中动态创建一个多级下拉菜单,并在下拉菜单中获取所选子菜单的值

时间:2019-05-09 17:16:11

标签: javascript arrays json reactjs

我将如何动态创建一个多级下拉菜单并获取所选的子菜单。

1 个答案:

答案 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}}