gpt4 book ai didi

backbone.js - 如何让主干将提交事件绑定(bind)到表单?

转载 作者:行者123 更新时间:2023-12-03 15:13:32 26 4
gpt4 key购买 nike

我正在使用以下代码来创建 View :

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>

当我绑定(bind)按钮时,会调用登录函数。绑定(bind)表单提交事件,登录函数不会被调用。如果 id 和 form 标签是模板的一部分,我也可以让表单绑定(bind),这不是我想要做的。

在这种情况下如何绑定(bind)表单提交?

最佳答案

"submit #login-form" : "login"

我认为 Backbone 只会在后代中搜索这个 id。所以它永远不会匹配你自己的 View 元素。你为什么不直接使用:
"submit": "login"

正如你为改变所做的那样。
为了确定,我会检查 Backbone 的代码。

编辑:
如果你放了一个选择器,Backbone 会调用
this.$el.on(event, selector, method);

代替
this.$el.on(event, method);

jQuery 的 on 方法会将选择器应用到 子孙仅元素的,不包括元素本身。

关于backbone.js - 如何让主干将提交事件绑定(bind)到表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15685718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com