React JS:在onChange下拉传递事件(Ant设计) [英] React JS: Pass event inside onChange drop down (Ant Design)
问题描述
我的表格中有一个下拉列表( https://ant.design/components/select)。在这个选择下拉列表中,我有onChange来调用一个函数。在'onChange'里面我想把这个事件作为参数传递给我的函数。问题是:当onChange发生时,只传递选定的值,但我想要整个事件。
I have a drop down in my form (https://ant.design/components/select). In this select drop down I have the onChange to call a function. Inside 'onChange' I want to pass the event as a parameter to my function. The problem is: when the onChange occurs, only the selected value is passed, but I want the entire event.
以下是代码:
export default class MyForm extends Component {
constructor() {
super();
this.handleOnChange = this.handleOnChange.bind(this);
}
handleOnChange = (event) => {
console.log(event); // here I'm receiving only the value selected (1 or 2)
}
render() {
render(
<Form>
<Select onChange={this.handleOnChange}>
<Option value="1">text 1</Option>
<Option value="2">text 2</Option>
</Select>
</Form>
)
}
}
在console.log()中,我只收到所选的值。有没有办法将整个事件对象传递给函数handleOnChange()?
In the console.log() I'm receiving only the selected value. Is there a way to pass the entire event object to the function handleOnChange()?
推荐答案
我找到了一个解决方案。只需使用:onSelect(),传递值和事件。
I found a solution. Just use: onSelect(), passing the value and the event.
handleOnChange = (value, event) => {
...code here
}
render() {
render(
<Form>
<Select onSelect={(value, event) => this.handleOnChange(value, event)}>
<Option value="1">text 1</Option>
<Option value="2">text 2</Option>
</Select>
</Form>
)
}
这篇关于React JS:在onChange下拉传递事件(Ant设计)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!