0

我在 dojo 中有以下代码,它使用相关菜单的几个 MenuItems 创建了一个 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 传递正确的 array[i]["Id"] 值

IE

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

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

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

.

.

.

ETC

谢谢

4

1 回答 1

2

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

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

var menuItem1 = new MenuItem({

                id: array[i]["Id"],

                label: "Option"+i,                

                onClick: function() { testfunc(this.id, this.label); }
            });
于 2012-06-28T07:55:13.570 回答