我有以下主干 View :
var EditBook = Backbone.View.extend({
el: '.page',
render: function (id) {
var that = this,
book = new Book({
id: id
});
book.fetch({
success: function (res) {
var template = _.template(editBookTpl, {
bookInfo: res
});
that.$el.html(template);
},
error: function () {
console.log('Error! Could not retrieve the book.');
}
});
},
events: {
// This event doesn't work...
'submit .editBookButton': 'editBook'
},
// I cannot seem to be able to bind the submit event to the .editBookButton
editBook: function (e) {
e.preventDefault();
console.log('clicked');
}
});
我所做的是使用 underscore.js 加载模板
模板内有一个表单,其中有一个提交按钮,该按钮的类为 .editBookButton
我想使用 Backbone 的事件,但它不起作用。
有什么想法可以解释为什么吗?
有没有办法确保事件仅针对此 View 中具有此类的元素触发?
我发现了类似的问题,但答案不完整。
最佳答案
按钮没有 submit
事件,只有表单有。
因此,您应该 try catch form
元素上的 submit
事件。
关于javascript - 提交时不会触发主干事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20661377/