Dojo 我可以从动态创建的dijit按钮onClick传递参数吗?

Dojo 我可以从动态创建的dijit按钮onClick传递参数吗?,dojo,Dojo,我想知道,我可以从动态创建的dijit按钮传递参数吗 function testcallfromDynamicButton (value) { alert(value); } var thisButton = new dijit.form.Button({ label : thelineindex , id : "I_del_butt"+thelineindex, name : "I_del_butt"+thelineindex, onClick : testcallfromDynami

我想知道,我可以从动态创建的dijit按钮传递参数吗

function testcallfromDynamicButton (value) {
   alert(value);
}

var thisButton = new dijit.form.Button({
label : thelineindex ,
id : "I_del_butt"+thelineindex,
name : "I_del_butt"+thelineindex,
onClick : testcallfromDynamicButton('test')
}).placeAt( thetabletd1 ) ;
似乎,这不起作用,我试着换成这个。它起作用了

function testcallfromDynamicButton () {
alert('test');
}

var thisButton = new dijit.form.Button({
  label : thelineindex ,
  id : "I_del_butt"+thelineindex,
  name : "I_del_butt"+thelineindex,
  onClick : testcallfromDynamicButton
}).placeAt( thetabletd1 ) ;

问题是,我想让函数知道单击了哪个按钮(因为所有按钮都是动态创建的,并且按钮id是由indexnumber生成的),所以我需要将按钮本身的id传递给函数。但通过onClick调用传递参数在Dijit中似乎不起作用。如何使其工作?

不用担心,这是一个非常常见的Javascript错误-事实上,它与Dojo无关

onClick需要一个函数对象,但实际上您正在执行
testcallfromDynamicButton('test')
,并将此函数调用的结果分配给它。例如,如果
testcallfromDynamicButton
返回“colacat”,onClick事件将被赋予该字符串!那显然不是你想要的

因此,我们需要确保为
onClick
提供一个函数对象,就像您在第二个示例中所做的那样。但我们也希望在函数执行时给它一个参数。方法是将函数调用包装在匿名函数中,如下所示:

var thisButton = new dijit.form.Button({
  label : thelineindex ,
  id : "I_del_butt"+thelineindex,
  name : "I_del_butt"+thelineindex,
  onClick : function() {
    testcallfromDynamicButton('test');
  }
}).placeAt( thetabletd1 ) ;
这样,
onClick
获得一个函数对象,并使用一个参数执行
testcallfromDynamicButton