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');
}
精彩评论