简体   繁体   English

在React中的html元素内添加onChange事件的正确方法是什么?

[英]What is correct way to add onChange event inside html element in React?

I am using fullcalendar library for the calendar feature. 我在日历功能中使用fullcalendar库。 It has one of the callback function to change the viewRender of the calendar. 它具有更改日历的viewRender的回调函数之一。 I am modifying it to change the title to dropdown select by appending the html element. 我正在修改它,以通过添加html元素将标题更改为下拉选择。

   viewRender: function(view) {
    let title = view.title;

    $("#calendar").find('.fc-toolbar .fc-left > h2').empty().append(
      "<div className='Select Select-month'>" +
        "<select id='months-tab' className='Select-input' onChange={this.handleChange.bind(this)}>" +
          "<option data-month='0'>January</option>" +
          "<option data-month='1'>February</option>" +
          "<option data-month='2'>March</option>" +
          "<option data-month='3'>April</option>" +
          "<option data-month='4'>May</option>" +
          "<option data-month='5'>June</option>" +
          "<option data-month='6'>July</option>" +
          "<option data-month='7'>August</option>" +
          "<option data-month='8'>September</option>" +
          "<option data-month='9'>October</option>" +
          "<option data-month='10'>November</option>" +
          "<option data-month='11'>December</option>" +
        "</select>" +
      "</div>"
    );
  },

How to bind handleChange event on the above appended select tag? 如何在上面附加的选择标记上绑定handleChange事件?

You will need to implement the event listener in componentDidMount() function in your component. 您将需要在组件中的componentDidMount()函数中实现事件侦听器。 componentDidMount will be called only once when the view is rendered for the first time. 首次渲染视图时,componentDidMount将仅被调用一次。

componentDidMount() {
   $('#months-tab').change(function() {
       //Logic to handle change event.
   }); 
}

Hope this helps. 希望这可以帮助。

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

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