[英]How to get current object reference inside a jquery callback function?
I have a javascript Object called aObject
and a fucntion inside it is used as a jQuery Callback function like this: 我有一个名为aObject
的javascript对象,其内部的函数用作jQuery回调函数,如下所示:
var aObject = {
aVariable : 'whatever value',
test : function(e) {
// Trying to access property. But doesn't work as expected since I am getting the DOM element i.e form, not the aObject reference
var temp = this.aVariable;
}
}
$('document').ready(function(){
$('#some-html-form').submit(aObject.test);
});
When I call the test method in aObject
, this refers to the form element that has been submitted. 当我在aObject
调用test方法时,这是指已提交的form元素。 I want to access current object from the test
callback function? 我想从test
回调函数访问当前对象?
I tried the below code as described in this answer but it did not work for me 我按照此答案中的说明尝试了以下代码,但对我不起作用
$(document).ready(function() {
$('#add_api_form').submit(api_cahce.handle_add_form_submit.bind(this));
});
bind with aObject
then you can access the variable. 与aObject
绑定,则可以访问变量。
var aObject = {
aVariable : 'whatever value',
test : function(e) {
var temp = this.aVariable;
}
}
$('document').ready(function(){
$('#some-html-form').submit(aObject.test.bind(aObject));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.