Javascript 使用 React JS 进行下拉的 OnChange 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28868071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OnChange event using React JS for drop down
提问by user544079
var MySelect = React.createClass({
change: function(){
return document.querySelector('#lang').value;
},
render: function(){
return(
<div>
<select id="lang">
<option value="select" onChange={this.change}>Select</option>
<option value="Java" onChange={this.change}>Java</option>
<option value="C++" onChange={this.change}>C++</option>
</select>
<p></p>
<p value={this.change}></p>
</div>
);
}
});
React.render(<MySelect />, document.body);
The onChangeevent does not work.
该onChange事件不起作用。
回答by Felix Kling
The change event is triggered on the <select>element, not the <option>element. However, that's not the only problem. The way you defined the changefunction won't cause a rerender of the component. It seems like you might not have fully grasped the concept of React yet, so maybe "Thinking in React"helps.
更改事件是在<select>元素上触发的,而不是在<option>元素上。然而,这并不是唯一的问题。您定义change函数的方式不会导致组件的重新渲染。看起来你可能还没有完全掌握 React 的概念,所以也许“Thinking in React”会有所帮助。
You have to store the selected value as state and update the state when the value changes. Updating the state will trigger a rerender of the component.
您必须将所选值存储为状态并在值更改时更新状态。更新状态将触发组件的重新渲染。
var MySelect = React.createClass({
getInitialState: function() {
return {
value: 'select'
}
},
change: function(event){
this.setState({value: event.target.value});
},
render: function(){
return(
<div>
<select id="lang" onChange={this.change} value={this.state.value}>
<option value="select">Select</option>
<option value="Java">Java</option>
<option value="C++">C++</option>
</select>
<p></p>
<p>{this.state.value}</p>
</div>
);
}
});
React.render(<MySelect />, document.body);
Also note that <p>elements don't have a valueattribute. React/JSX simply replicates the well-known HTML syntax, it doesn't introduce custom attributes (with the exception of keyand ref). If you want the selected value to be the content of the <p>element then simply put inside of it, like you would do with any static content.
另请注意,<p>元素没有value属性。React/JSX 只是简单地复制了众所周知的 HTML 语法,它没有引入自定义属性(除了key和ref)。如果您希望所选值成为<p>元素的内容,那么只需将其放入其中,就像处理任何静态内容一样。
Learn more about event handling, state and form controls:
了解有关事件处理、状态和表单控件的更多信息:
回答by Kirk Strobeck
import React, { PureComponent, Fragment } from 'react';
import ReactDOM from 'react-dom';
class Select extends PureComponent {
state = {
options: [
{
name: 'Select…',
value: null,
},
{
name: 'A',
value: 'a',
},
{
name: 'B',
value: 'b',
},
{
name: 'C',
value: 'c',
},
],
value: '?',
};
handleChange = (event) => {
this.setState({ value: event.target.value });
};
render() {
const { options, value } = this.state;
return (
<Fragment>
<select onChange={this.handleChange} value={value}>
{options.map(item => (
<option key={item.value} value={item.value}>
{item.name}
</option>
))}
</select>
<h1>Favorite letter: {value}</h1>
</Fragment>
);
}
}
ReactDOM.render(<Select />, window.document.body);
回答by ahota
React Hooks (16.8+):
反应钩子(16.8+):
const Dropdown = ({
options
}) => {
const [selectedOption, setSelectedOption] = useState(options[0].value);
return (
<select
value={selectedOption}
onChange={e => setSelectedOption(e.target.value)}>
{options.map(o => (
<option value={o.value}>{o.label}</option>
))}
</select>
);
};
回答by Abolfazl Miadian
Thank you Felix Kling, but his answer need a little change:
谢谢 Felix Kling,但他的回答需要稍作改动:
var MySelect = React.createClass({
getInitialState: function() {
return {
value: 'select'
}
},
change: function(event){
this.setState({value: event.target.value});
},
render: function(){
return(
<div>
<select id="lang" onChange={this.change.bind(this)} value={this.state.value}>
<option value="select">Select</option>
<option value="Java">Java</option>
<option value="C++">C++</option>
</select>
<p></p>
<p>{this.state.value}</p>
</div>
);
}
});
React.render(<MySelect />, document.body);
回答by Satish
If you are using select as inline to other component, then you can also use like given below.
如果您使用 select 作为其他组件的内联,那么您也可以使用如下所示。
<select onChange={(val) => this.handlePeriodChange(val.target.value)} className="btn btn-sm btn-outline-secondary dropdown-toggle">
<option value="TODAY">Today</option>
<option value="THIS_WEEK" >This Week</option>
<option value="THIS_MONTH">This Month</option>
<option value="THIS_YEAR">This Year</option>
<option selected value="LAST_AVAILABLE_DAY">Last Availabe NAV Day</option>
</select>
And on the component where select is used, define the function to handle onChange like below:
在使用 select 的组件上,定义处理 onChange 的函数,如下所示:
handlePeriodChange(selVal) {
this.props.handlePeriodChange(selVal);
}

