我有一个被调用的 javascript 对象aObject
,其中的一个功能用作 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);
});
当我调用 中的测试方法时aObject
,这是指已提交的表单元素。我想从test
回调函数访问当前对象?
我尝试了这个答案中描述的以下代码,但它对我不起作用
$(document).ready(function() {
$('#add_api_form').submit(api_cahce.handle_add_form_submit.bind(this));
});
绑定aObject
然后您可以访问该变量。
var aObject = {
aVariable : 'whatever value',
test : function(e) {
var temp = this.aVariable;
}
}
$('document').ready(function(){
$('#some-html-form').submit(aObject.test.bind(aObject));
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句