Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
道に迷いました。ループ変数を AJAX .done() 呼び出しに渡すにはどうすればよいですか?
for (var i in obj) { $.ajax(/script/).done(function(data){ console.log(data); }); }
明らかに、そうするなら、console.log(i+' '+data) 反復objごとにオブジェクトの最後のキーを返します。ドキュメンテーションは私を失敗させます。
console.log(i+' '+data)
obj
(自己実行関数を介して)クロージャを使用して、i次のようにループの呼び出しごとにの値をキャプチャできます。
i
for (var i in obj) { (function(index) { // you can use the variable "index" here instead of i $.ajax(/script/).done(function(data){ console.log(data); }); })(i); }