我想新建obj例如newForm = new MyForm(...);
然后我想把点击事件绑定到一个按钮上。
如何从点击事件中回调"newForm"?
示例代码(绑定失败):
var MyForm = function(data, targetUrl, targetObj, preload, callback){
this.data = data;
this.preload = preload;
this.targetUrl = targetUrl;
this.targetObj = targetObj;
this.callback = callback;
this.this = this;
this.bind();
};
MyForm.prototype.submit = function(){
this.preload;
$.ajax({
type: "POST",
url: this.targetUrl,
data: this.data
}).done(function(responseData) {
if(typeof this.callback === 'function'){
return this.callback(responseData);
}
});
};
MyForm.prototype.bind = function(newTargetObj){
if(typeof newTargetObj === 'object'){
this.targetObj = newTargetObj;
}
this.targetObj.click(this.clickEvent());
}
MyForm.prototype.clickEvent = function(){
console.log(this);
console.log(this.targetForm);
// this.targetForm.submit();
alert('click!');
};
我目前无法对此进行测试,但我相信您应该能够将对象传递到您为点击事件连接的事件中。如下所示。
MyForm.prototype.bind = function(newTargetObj){
if(typeof newTargetObj === 'object'){
this.targetObj = newTargetObj;
}
this.targetObj.click(this.clickEvent(this.targetObj));
}
MyForm.prototype.clickEvent = function(sender){
console.log(this);
console.log(this.targetForm);
// this.targetForm.submit();
alert('click!');
};