我使用下面的代碼來創建視圖:如何獲得骨幹將提交事件綁定到表單?
LoginForm = Backbone.View.extend({
tagName :"form"
,id : "login-form"
,className :"navbar-form"
,initialize: function() {
this.model = new StackMob.User();
this.render();
}
,render: function() {
$(this.el).html(this.template());
return this;
}
,events : {
"change" : "change"
,"submit #login-form" : "login"
}
,login : function(event) {
event.preventDefault();
var self = this;
this.model.login(true, {
success: function(model) {
app.alertSuccess("User logged in");
self.render();
}
,error: function(model, response) {
app.alertError("Could not login user: " + response.error_description);
}
});
event.currentTarget.checkValidity();
return false;
}
// rest of code
而且模板:
<input name="username" class="span2" type="email" placeholder="Email" required >
<input name="password" class="span2" type="password" placeholder="Password" required >
<button id="login-button" type="submit" class="btn">Sign in</button>
當我綁定按鈕,登錄函數被調用。綁定表單提交事件時,登錄函數不會被調用。如果表單標籤是模板的一部分,我也可以獲取表單綁定,這不是我想要在這裏完成的。
如何在這種情況下綁定表單提交?
相關文檔隱藏在['delegateEvents'](http:// backbonejs。org /#View-delegateEvents)documentation:「省略'selector'會導致事件綁定到視圖的根元素'(this.el)'。」 –
不幸的是,這並不排除他在做什麼。那麼,我想這仍然是足夠的信息。 – Loamhoof
提交沒有選擇工作! –