简体   繁体   English

如何为每个自动生成的按钮分配不同的ID?

[英]How to assign a different ID to every auto-generated button?

I use Facebook API to get all groups from user and then another function with FQL to make a different div for each group, and assign to each div a button, which links to another page, something like: 我使用Facebook API从用户获取所有组,然后使用FQL的另一个函数为每个组创建一个不同的div,并为每个div分配一个按钮,该按钮链接到另一个页面,例如:

$('.groupButton').click(function() {  
  window.location ='anotherpage.html?groupid=' + groupid;
});

But on every button it assigns the last button's groupid. 但是在每个按钮上,它分配了最后一个按钮的组ID。 I don't know where's the mistake. 我不知道哪里出了错。

The 2 functions: 2个功能:

        function getGroupsIds(){
            document.getElementById('content_text').innerHTML = "";
            FB.api('/me/groups',function(resp){
                for (var i=0, l=resp.data.length; i<l; i++){ 
                getGroupInfo(resp.data[i].id);
                }
            });
        }


        function getGroupInfo(groupid){
            FB.api({
                method: 'fql.query',
                query: 'Select name, description, gid, pic_cover from group where gid=' + groupid
            }, function(resp){
                var content_text = document.getElementById('content_text');
                var group = new Array(); 
                for (var i=0, l=resp.length; i<l; i++) 
                {


                    groupinput = document.createElement('button');
                    groupinput.className = "groupButton";            
                    groupinput.innerHTML = "Vizualise";

                    $('.groupButton').click(function() {  
                    window.location ='pages/appviz.html?groupid=' + groupid;
                    });


                    groupdiv.appendChild(groupinput);
                    content_text.appendChild(groupdiv);

                }
            });
    }

From where you get the groupid? 从哪里获得组ID? It is not clear. 不清楚。 You can assign the groupid in the button as id. 您可以将按钮中的groupid分配为id。 Then get the value when you click the button. 然后在单击按钮时获取值。

$('.groupButton').click(function() {
groupid = this.id;
window.location ='anotherpage.html?groupid=' + groupid;
});

And the input will be like 输入会像

<input type='button' id='1' value='Profile' class='groupButton' />

Hope this will help you. 希望这会帮助你。

好吧,我使用了javascript,并且有效。

 groupinput.onclick = function(){ window.location.replace('pages/appviz.html?groupid=' + groupid);};

You are reassigning the click handler repeatedly in the for loop. 您正在for循环中反复分配单击处理程序。 This is causing the all buttons with the groupButton class to navigate to the last groupid you processed in the loop. 这导致groupButton类的所有按钮导航到循环中处理的最后一个groupid

for (var i=0, l=resp.length; i<l; i++) {
    // this gets reassigned resp.length times
    $('.groupButton').click(function() {
        window.location ='pages/appviz.html?groupid=' + groupid;
    }
}

What you probably want is 您可能想要的是

function getGroupInfo(groupid){
    FB.api({
        method: 'fql.query',
        query: 'Select name, description, gid, pic_cover from group where gid=' + groupid
    },
    function(resp) {
        ...
        for() { ... }

        groupdiv.appendChild(groupinput);
        content_text.appendChild(groupdiv);

        // assign click handler after building the elements
        $('.groupButton').click(function() {
            window.location = 'pages/appviz.html?groupid=' + this.id;
        });
    }

The $('.groupButton') is the set of all elements with the groupButton class (see Class Selector ). $('.groupButton')groupButton类的所有元素的groupButton (请参见Class Selector )。 So you need this.id to get the id of the instance of button you clicked. 因此,您需要this.id来获取您单击的按钮实例的id

Most probably just remove the following code from inside of loop 最有可能只是从循环内部删除以下代码

 $('.groupButton').click(function() {  
                window.location ='pages/appviz.html?groupid=' + groupid;
                });

And put it outside of the function function getGroupInfo(groupid) , because the click function is only to done the action. 并将其放在函数function getGroupInfo(groupid) ,因为click函数仅用于执行操作。 Try and let us know its worked or not. 尝试让我们知道它是否有效。

function getGroupInfo(groupid){
            FB.api({
                method: 'fql.query',
                query: 'Select name, description, gid, pic_cover from group where gid=' + groupid
            }, function(resp){
                var content_text = document.getElementById('content_text');
                var group = new Array(); 
                for (var i=0, l=resp.length; i<l; i++) 
                {


                    groupinput = document.createElement('button');
                    groupinput.className = "groupButton";            
                    groupinput.innerHTML = "Vizualise";
                    groupinput.id = "g"+groupid;

                    groupdiv.appendChild(groupinput);
                    content_text.appendChild(groupdiv);

                }
            });
    }

$('.groupButton').click(function() {  
groupid = this.id;
 window.location ='pages/appviz.html?groupid=' + groupid.substr(1);
});

Try this 尝试这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM