2014-02-11 58 views
0

我只是試圖在沒有使用template的按鈕上實現click
HTML使用Backbone View組件註冊靜態UI元素事件

<div id="transfer"> 
      <input type="text" placeholder="From Address" id="fromAddress" /> 
      <input type="text" placeholder="To Address" id="toAddress" /> 
      <input type="text" placeholder="Amount" id="amount" /> 
      <input type="button" id="transferMoney" value="Transfer"/> 
     </div> 

骨幹查看

var TransferView = Backbone.View.extend({ 
    events: { 
     "click #transferMoney": "sendMoney" 
    }, 
    sendMoney: function() { 
     alert(); 
     console.log($("#fromAddress").val()); 
     //this.modeltransferMoney); 
    } 
}); 

var transferView = new TransferView({mdoel: transferMoney}); 
transferView.render(); 

sendMoney()當我點擊#transferMoney按鈕不會被調用。
我是否總是必須使用template創建按鈕?

回答

1

這是因爲事件散列在視圖範圍上受到限制(請參閱http://backbonejs.org/#View-delegateEvents)。嘗試

var TransferView = Backbone.View.extend({ 
    initialize: function(){ 
     $("#transferMoney").on("click", this.sendMoney); 
    }, 
    sendMoney: function() { 
     alert(); 
     console.log($("#fromAddress").val()); 
     //this.modeltransferMoney); 
    } 
}); 

var transferView = new TransferView({mdoel: transferMoney}); 
transferView.render();