How do I get backbone to bind the submit event to a form?

本小妞迷上赌 提交于 2019-11-30 17:27:33
Loamhoof
"submit #login-form" : "login"

I think Backbone will search for this id among the descendants only. So it will never match your own view element. Why don't you just use:

"submit": "login"

As you did for change.
Gonna check Backbone's code just to be sure.

Edit:
If you put a selector, Backbone will call

this.$el.on(event, selector, method);

instead of

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

And the on method of jQuery will instead apply the selector to the descendants of the element only, excluding the element itself.

You're using Backbone wrong. So what you're going to want to do,

template: my_template_string,
render: function () {
    this.el.innerHTML = this.template();
},
events: {
    "submit #login-form": function (event) {}
}

Where this.template is set to

<form id="login-form" class="navbar-form">
    <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>
</form>

And doesn't that only make sense? Why would you want the id and classname to be separated from the input elements? BTW, you can still do the naked catchall on submit, but only in my method will,

  • the <form> class, and <form> attribute be tied to the form's template, and not just the backbone view,
  • will you be explicitly capturing the right submit,
  • can you ever support multiple submit events (in the event one template has two forms).

Maybe When you bind on the form submit event, it is only submit the form will fire the 'submit' event. you could add below code and try it again.

$('#id').submit(function(ev) {

});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!