Backbone.js/handlebar按钮单击事件未触发
我有一个有按钮的把手,像这样:Backbone.js/handlebar按钮单击事件未触发,backbone.js,backbone-views,handlebars.js,Backbone.js,Backbone Views,Handlebars.js,我有一个有按钮的把手,像这样: <button id="addLine" type="image" class="AddButtonClass" title="Add New Line" onclick = "addNewBlock('{{blockId}}');"></button> var template = window.app.getTemplate('myHandlebar'); var budgetBlock = $(temp
<button id="addLine" type="image" class="AddButtonClass" title="Add New Line" onclick = "addNewBlock('{{blockId}}');"></button>
var template = window.app.getTemplate('myHandlebar');
var budgetBlock = $(template({blockId: guid}));
$("#BudgetTable tbody").append(budgetBlock);
addNewBlock: function(getId){
alert(getId);
},
然后我有一个这样的函数:
<button id="addLine" type="image" class="AddButtonClass" title="Add New Line" onclick = "addNewBlock('{{blockId}}');"></button>
var template = window.app.getTemplate('myHandlebar');
var budgetBlock = $(template({blockId: guid}));
$("#BudgetTable tbody").append(budgetBlock);
addNewBlock: function(getId){
alert(getId);
},
问题是车把事件上的按钮从未触发。相反,我得到一个错误“UncaughtReferenceError:addNewBlock未定义”
为什么我不能将onclick事件指定给车把中的按钮?在创建模板时,是否有一种方法可以动态地向按钮添加事件
谢谢试试这个:
<button id="addLine" type="image" class="AddButtonClass" title="Add New Line" data-id="{{blockId}}"></button>
或者您可以在主干中创建一个事件
events: {
'click #addLine' : 'addNewBlock'
},
addNewBlock: function(e){
console.log( $(e.currentTarget).data("id"));
}
我有点希望能在车把上找到一种方法,但是你的第一个解决方案也能做到。谢谢!很高兴帮助你:)@jason