2013-07-30 35 views
0

我試圖設置一個點擊事件動態地dijit /窗體/按鈕。 但是,我嘗試的方法似乎不起作用。動態設置一個點擊事件dojo按鈕

<button id = "newActButton" data-dojo-type="dijit/form/Button" 
    type = "button" 
    data-dojo-props="iconClass: 'newActButtonIcon', label: 'New Act'"></button> 


dijit.byId("newActButton").set("onClick", newActButtonOnClick()); 

我有一個函數newActButtonOnClick(),我想要觸發。

+1

什麼Dojo的版本? –

+0

1.7.0謝謝 – Sandy

+1

任何你不使用dojo/on事件處理程序的原因? – tik27

回答

1

您可以嘗試這樣的事:

require(["dojo/on", "dojo/dom", "dojo/domReady!"], 
    function(on, dom) { 
     var newActButton = dom.byId("newActButton"); 

     on(newActButton, "click", newActButtonOnClick); 
}); 

Here's tutorial

jsfiddle

+2

一些建議 - 使用'dijit.byId',因爲正在使用的按鈕是dijit小部件,並且在全局模塊上使用'Widget#'('dijit.byId('myButton')。on 'click',function(){});' – BuffaloBuffalo

+1

@BuffaloBuffalo您的建議是否與1.8版本相關?好的OP在[出現在Dojo 1.8中]使用Dojo 1,7和dijit._Widget#(http://dojotoolkit.org /api/1.8/dijit/_Widget)和[不會在1.7上工作](http://dojotoolkit.org/api/1.7/dijit/_Widget),恐怕 – lifus

+0

哎呀,你是對的。我沒有真正開始使用'on' /'emit'事件樣式,直到1.8(與dojo/connect api相比)。 – BuffaloBuffalo

0

它不工作,因爲你在代碼中有一個錯誤(by.Id VERSUS byId):

dijit.by.Id("newActButton").set("onClick", newActButtonOnClick()); 

應該是

dijit.byId("newActButton").set("onClick", newActButtonOnClick()); 

編輯

試試這個:

require(["dojo/parser", "dijit/form/Button"]); 

<button data-dojo-type="dijit/form/Button" type="button">Click me too! 
    <script type="dojo/on" data-dojo-event="click" data-dojo-args="evt"> 
     require(["dojo/dom"], function(dom){ 
      dom.byId("result2").innerHTML += "Thank you! "; 
     }); 
    </script> 
</button> 
<div id="result2"></div> 
+0

對不起,這是一個錯字,它不能與dijit.byId一起工作 – Sandy