Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js/handlebar按钮单击事件未触发_Backbone.js_Backbone Views_Handlebars.js - Fatal编程技术网

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