[英]JavaScript: Why isn't my closure working?
The following code is only assigning the value of the last .enter_form input
to the last MYAPP.list[0].responses[MYAPP.score.round].form[key]
(where key
is the only thing that varies). 以下代码仅将最后一个.enter_form input
的值分配给最后一个MYAPP.list[0].responses[MYAPP.score.round].form[key]
(其中key
是唯一变化的东西)。 I think it's because only the last value of the key is being passed to addEntry()
, but I can't figure out how to get around that. 我认为这是因为只有键的最后一个值被传递给addEntry()
,但我无法弄清楚如何解决这个问题。
$('.enter_form input').each(function() {
var key = $(this).attr('id');
var val = $(this).val();
userDict[key] = val;
MYAPP.list[0].responses[MYAPP.score.round].form = [];
function addEntry() {
return function(k) {
MYAPP.list[0].responses[MYAPP.score.round].form[k] = {'entry': userDict[k]};
}(key);
}
addEntry();
}
Your addEntry function is redundant since each iteration is already run inside it´s own scope so key
and val
are preserved properly (hope that explanation makes sense). 你的addEntry函数是多余的,因为每次迭代都已在你自己的范围内运行,所以key
和val
被正确保存(希望解释有意义)。 Also the array you where inserting into was overwritten each iteration as well, so at the end of the .each()
you end up with an array with only 1 value. 您插入的数组也会在每次迭代时被覆盖,因此在.each()
末尾,您最终会得到一个只有1个值的数组。 It should also be an object rather then an array, even if the id's are numerical. 即使id是数字,它也应该是一个对象而不是数组。
// you where overwriting this each iteration
MYAPP.list[0].responses[MYAPP.score.round].form = {};
$('.enter_form input').each(function() {
var el= $(this); // cache instead of creating a new jQuery object each time
var key = el.attr('id');
var val = el.val();
userDict[key] = val;
MYAPP.list[0].responses[MYAPP.score.round].form[key] = {'entry': userDict[key]};
}); // ); was also missing
Should work. 应该管用。
It's a bit hard to work out what it's meant to do, but I think this is probably it: 确定要执行的操作有些困难,但我认为可能是这样:
MYAPP.list[0].responses[MYAPP.score.round].form = [];
$('.enter_form input').each(function() {
var $this = $(this),
key = this.id,
val = $this.val();
userDict[key] = val;
MYAPP.list[0].responses[MYAPP.score.round].form[key] = {
'entry': val
};
});
That's based on your saying that "... key
is the only thing that varies" (presumably $(this).val()
also varies, but I took your point). 这是基于你的说法“...... key
是唯一不同的东西” (大概$(this).val()
也有所不同,但我接受了你的观点)。 It will add entries to MYAPP.list[0].responses[MYAPP.score.round].form
for each of the form's input
id
s, as well as adding them to the userDict
map. 它将为表单的每个input
id
添加到MYAPP.list[0].responses[MYAPP.score.round].form
条目,并将它们添加到userDict
映射。
As a side note, if the id
values on the input
elements aren't purely numeric, then I suspect you want to start with a blank object : 作为旁注,如果input
元素上的id
值不是纯数字,那么我怀疑你想要一个空白对象 :
MYAPP.list[0].responses[MYAPP.score.round].form = {};
// ^^-- change is here
...rather than an empty array: ...而不是一个空数组:
MYAPP.list[0].responses[MYAPP.score.round].form = [];
...although since arrays are objects, it works even if you're adding non-numeric properties. ...尽管由于数组是对象,所以即使您添加非数字属性也可以使用。
Off-topic : No need for $(this).attr('id')
. 离题 :不需要$(this).attr('id')
。 Just use this.id
. 只需使用this.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.