【问题标题】:Dojo CompoButton - onClick event of MenuItem issue with its functionDojo CompoButton - MenuItem 的 onClick 事件及其功能问题
【发布时间】:2012-06-24 03:28:59
【问题描述】:

我在 dojo 中使用以下代码创建了一个包含多个相关菜单的 MenuItem 的 ComboButton:

    testfunc = function (input) {
                                //Code that uses input
            }

            var menu = new Menu({
                id: "saveMenu"
            });
            //array has 10 json objects and each object has an "Id" field
            for(var i=0; i<10; i++) {

            var temp = array[i]["Id"];

            var menuItem1 = new MenuItem({
                label: "Option"+i,                
                onClick: function() { testfunc(temp); }
            });

            menu.addChild(menuItem1);
            }

            var comboButton = new ComboButton({
                optionsTitle: "Options",    
                label: "Combo",
                dropDown: menu,
 --->         onClick:function(){ console.log("Clicked ComboButton"); }
            }, "combo");

            comboButton.startup();
            menu.startup();

我需要每个 MenuItem 的 onClick 函数在 testfunc 中传递不同的变量。特别是 array[i]["Id"] 的值,它是一个包含 10 个 json 对象的数组。使用上面的代码,在所有 MenuItems 的所有 testfunc 函数中传递的参数是最后一个。

是否可以为每个 MenuItem 传递正确的数组 [i]["Id"] 值

MenuItem0 -> onClick: testfunc(array[0]["Id"])

MenuItem1 -> onClick: testfunc(array[1]["Id"])

MenuItem2 -> onClick: testfunc(array[2]["Id"])

.

.

.

谢谢

【问题讨论】:

    标签: dojo


    【解决方案1】:

    我找到了解决问题的方法,我想我应该分享它。

    问题已通过在 menuItem 的 onClick 事件的 testfunc 中使用“this”标记传递参数来解决,例如

    var menuItem1 = new MenuItem({
    
                    id: array[i]["Id"],
    
                    label: "Option"+i,                
    
                    onClick: function() { testfunc(this.id, this.label); }
                });
    

    【讨论】:

      猜你喜欢
      • 2016-03-09
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 2016-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多