我试图在 JQuery 事件回调中引用 this
(即 ReactJS 组件):
var Component = React.createClass({
func1: function(){
$("#multi").multiselect({
onChange: function(a, b){
this.test();
},
});
},
test: function(){
console.log("Calling a react component function");
}
...
});
然而它说:
TypeError: this.test is not a function
如何在 JQuery 事件回调函数中引用 this
?
最佳答案
您需要将 onChange
回调绑定(bind)到上下文:
onChange: function(a, b) {
this.test();
}.bind(this)
你也可以使用箭头函数:
onChange: (a, b) => {
this.test()
}
关于javascript - ReactJS:在 JQuery 事件监听器函数中调用 'this',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35785820/