你如何在React中获得select-option的值?

时间:2017-12-09 23:48:52

标签: reactjs jsx

我尝试使用onChange,onClick和ref,但没有一个可以为我提供我选择的选项的值

<select ref={input => { this.handleInput(input.value) } }>
    <option value="1">Blah</option>
    <option value="2">Blah2</option>
    <option value="3">Blah3</option>
</select>

如您所见,input.value显示未定义。我不知道除了将ref更改为onChange并且onClick不起作用,否则获取选项值

1 个答案:

答案 0 :(得分:0)

请尝试class App extends React.Component { constructor(props){ super(props) this.state = { result: '' }; } handleSelectChange = (event) => { this.setState({ result: event.target.value }) } render() { return ( <div> <select onClick={this.handleSelectChange}> <option value="1">Blah</option> <option value="2">Blah2</option> <option value="3">Blah3</option> </select> {this.state.result} </div> ); } } ReactDOM.render( <App />, document.getElementById('container') );,如下所示:

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="container">
    <!-- This element's contents will be replaced with your component. -->
</div>
// _schema is known
Entity ent = element.GetEntity(_schema);
if (ent?.Schema != null)
{
// code to retrieve extensible storage here
// produce correct result
}