我有一個對象。它具有一些屬性,DOM元素的初始化函數以及該DOM元素的事件處理程序。在事件處理程序上使用bind(this)
我想我的事件處理程序有權訪問該對象的屬性。我正在使用.bind(this),但它說「不能調用方法」綁定「未定義」。我究竟做錯了什麼?
var SignUpForm2 = {
eForm: null,
eEmailInput: null,
ePasswordInput: null,
signUpURL: null,
email: null,
password: null,
handleFormSubmit: function() {
e.preventDefault();
this.email = this.eEmailInput.val();
this.password = this.ePasswordInput.val();
$.ajax({
type: "POST",
url: this.signUpURL,
data: {
email: this.email,
password: this.password
},
success: function(response){
}
});
},
init: function(eForm) {
this.eForm = eForm;
this.eEmailInput = this.eForm.find('input[name="email"]');
this.ePasswordInput = this.eForm.find('input[name="password"]');
this.signUpURL = "/index.php/ajax/user-sign-up-via-email";
this.eForm.submit(this.handleFormSubmit.bind(this));
},
}
它的工作原理:http://jsfiddle.net/v3678/1/ – Cherniv
@Cherniv - 你怎麼知道?它看起來不像你可以提交該表格...... –
DOM'form.submit()'方法不帶參數。 – h0tw1r3