【问题标题】:Can I pass parameter from a dynamic created dijit button onClick?我可以从动态创建的 dijit 按钮 onClick 传递参数吗?
【发布时间】:2011-05-23 16:19:17
【问题描述】:

我想知道,我可以从动态创建的 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 中不起作用。我怎样才能让它工作?

【问题讨论】:

    标签: dojo


    【解决方案1】:

    不用担心,这是一个非常常见的 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 执行时带有一个参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多