开发者

Jquery dynamic selector on Click

I need "i" click functions depending on variable unknown l开发者_StackOverflow中文版ength:

for(i=0;i<=unknownLength;i++)
{$("#group_link_"+i).click(function()
{//asynchronous call to a Web Service for the specific content on group_link_i div, here I will use $("#group_link_"+i).val()}
 );}

This code is not working. However, the same code, if i delete the loop and replicate the code above "i" times, it's working perfectly.

How can I acomplish this functionality using a loop?

Many thanks


A loop isn't necessary here, just use a better selector like the 'starts with' selector

$("a[id^='group_link_']").click(function() {
    ///do something with $(this) - which will be each element
});


FIXED:

for(var i = 0;; i++) {
    var element = $("#group_link_" + i);
    if (element.length) {
        element.click(function() {

        });        
    } else {
        break;
    }
}


Thanks Naspinski. I sort out the problem as u told me. Many Thanks!. 100% working:

        $("a[id^='group_link_']").click(function()
    {
            $(this).parent().next().empty();

            $().SPServices({
              operation: "GetUserCollectionFromGroup",
              groupName: $(this).text(),
              async: false,
              completefunc: processUsersGroups
           }); /*close().SPServices({ */

           function processUsersGroups (xData, status)
           {
                var i = 0;
                var append_data = "";
                var email_user="";
                $("a[id^='group_link_']").parent().next().append("<ul class=\"contact_list\"><div id=\"paginator\" class=\"paginator_accordion\">");
                $(xData.responseXML).find("[nodeName=User]").each
                (
                    function()
                    {   
                        if ($(this).attr("Email")!="")
                        {
                            email_user=$(this).attr("Email");
                        }
                        else
                        {
                            email_user="<i>Not Available in Active Directory</i>";
                        }

                        append_data = append_data +"<li class=\"contact\"><div class=\"user\"></div><div class=\"user_name\">"+$(this).attr("Name")+"</div><div class=\"mail\"></div><div class=\"user_mail\">"+email_user+"</div><div class=\"thick\"></div><div class=\"minus\"></div><div class=\"plus\"></div></li>";
                        i++;
                    }
                )           
                $("a[id^='group_link_']").parent().next().append(append_data);
                if(i==0)
                {
                    $("a[id^='group_link_']").parent().next().append("There's no users available in this group");
                }
                else
                {
                    $("a[id^='group_link_']").parent().next().append("</div></ul>");
                }
           }
           $(function(){ $("#paginator").pagination(); });
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜