how javascript excutes callback with parameters -
i in trouble javascript‘s callback,my code seems simple:
var =0; (; < listsize+1; i++) { var content = "content"+i; $("#" + content).focus(function () { $("#" + content).keydown(check(new number(i))); $("#" + content).keyup(check(new number(i))); }); }
where lisetsize=3
in test case , content
html element's id
and callback function check(my_num)
is:
function check(my_num) { var content = "content"+my_num; }
then try trigger function through keyboard input.
however,i got result content=content4
time via broswer's debugger,even though listening element content0
i have try anyway such $.extend({},i)
$.extend(true,{},i)
it make no difference
now have no idea problem,how can pass value no reference callback function's parameter?
you're not declaring handlers correctly.
replace:
$("#" + content).keydown(check(new number(i))); $("#" + content).keyup(check(new number(i)));
with:
$("#" + content).keydown(function(){check(new number(i));}); $("#" + content).keyup(function(){check(new number(i));});
what need pass keyup
, keydown
, functions need called when keyboard events happen.
what were passing keyup
, keydown
, results of calling check(new number(i))
.
also, since you're declaring these in loop, you'll want copy number new variable, in order reference current loop iteration's value:
$("#" + content).focus(function () { var currentnumber = i; $("#" + content).keydown(function(){check(currentnumber);}); $("#" + content).keyup(function(){check(currentnumber);}); });
Comments
Post a Comment