简体   繁体   English

触发反应选择 onchange

[英]Trigger react select onchange

What is the best way to trigger onchange even on a select field in react?即使在反应中的选择字段上触发 onchange 的最佳方法是什么?

Unfortunately, I have to use plain javascript to format and set a value programmatically but that doesn't fire React events.不幸的是,我必须使用普通的 javascript 以编程方式格式化和设置一个值,但这不会触发 React 事件。

What's the best way to fire those events?触发这些事件的最佳方法是什么?

Say I want to set a value of my select manually by document.querySelector(selector).value = value;假设我想通过document.querySelector(selector).value = value;手动设置我的选择document.querySelector(selector).value = value; how can I trigger the onchange event?如何触发 onchange 事件?

Edit: clarifying the question编辑:澄清问题

Its same as in normal JS and HTML它与普通的 JS 和 HTML 相同

<select 
        value={this.state.selectValue} 
        onChange={this.handleChange} 
      >

So, handleChange is the function inside that react component itself.因此,handleChange 是反应组件本身内部的函数。 You can update component's state inside handleChange function.您可以在 handleChange 函数中更新组件的状态。 Make sure you use value={this.state.selectValue} too.确保您也使用 value={this.state.selectValue} 。 This way you are binding state to the select component.通过这种方式,您将状态绑定到选择组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM