Dojo.Connect with click event for div element inside loop

只愿长相守 提交于 2019-12-11 18:07:56

问题


I'm trying to show alert message including click event on div element that are sought through a loop. The problem is that in any div I click, it is only displayed the alert of the last element. How can I solve? I do not understand the logic being performed.

                for (var i = 0; i < this.legend.layerInfos.length; i++)
                {   
                    var sNomeDiv = "";
                    var sMensagem = ""; 

                    if (this.legend.layerInfos[i].layer.visible)
                    {
                        sNomeDiv = this.legend.id + "_" + this.legend.layerInfos[i].layer.id;                                               

                        if (this.legend.layerInfos[i].layer.description == "" || this.legend.layerInfos[i].layer.description == "undefined" || this.legend.layerInfos[i].layer.description == "null")
                        {
                            sMensagem = "Nenhuma informação cadastrada para a camada " + this.legend.layerInfos[i].title;
                        }
                        else
                        {
                            sMensagem = this.legend.layerInfos[i].layer.description;
                        }


                        //Always display an alert with the text of the last element of the loop
                        dojo.connect
                        (
                            dojo.byId(sNomeDiv), 
                            "onclick", 
                            function()
                            {      
                                alert(sMensagem + " --> " + sNomeDiv);
                            }
                        );
                    }
                }

来源:https://stackoverflow.com/questions/39521219/dojo-connect-with-click-event-for-div-element-inside-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!