开发者

Dynamic links function parameters problem jquery

I'm having problems with some dynamic elements that I'm creating with Jquery.

for(var i = 1; i <= totalPages; i++) {
            var link = $(document.createElement('a')).html(i.toString());
            link.attr("id", "link" + i);
            link.click(function ()
            {
                nextPage(i);
            });
            link.css('cursor', 'pointer');
        }

When I see the page and click in the links they always send the parameter with the last value of the v开发者_如何学Pythonariable "i" (always totalPages + 1).

Anyone knows what am I doing wrong?


Try this

for(var i = 1; i <= totalPages; i++) {
    var link = $(document.createElement('a')).html(i.toString());
    link.attr("id", "link" + i);
    link.data("index", i);
    link.click(function ()
    {
        nextPage($(this).data("index"));
    });
    link.css('cursor', 'pointer');
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜