javascript - Closures in a for loop -


Closers in a loop are causing me problems I think I have to create another function to solve the problem Returns the function for, but I can not work with it's jQuery code

Here is a basic problem in a simple form:

  function fu (val) {alert (val); } (Var i = 0; i & lt; 3; i ++) {$ ('# button' + i). Click (function () {foo (i);}); }  

Naturally, clicking on any of three buttons will be given a warning 3. I want the functionality that will be warned by clicking on button 1, button 2 2 Will say etc.

How can I do this?

See the method. / P>

  $ ('# button' + i) .bind ('click', {button: i}, function (event) {foo (event.data.button);});  

From Docs:

Alternate EventData Parameter is not commonly used. Upon granted, this argument allows us to give additional information to the handler.

This parameter has to work around issues that are close to an easy use.

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -