[英]Am I missing something? My values don't get stored into my array
I have this 我有这个
var selected = []
$('#SelectBoxContainer .DDLs :selected').each(function (i, selected)
{
alert($(selected).val());
selected[i] = $(selected).val();
});
My alert is telling me that it is going through this loop and getting the select box values. 我的警报告诉我它正在经历此循环并获取选择框值。 Yet once everything is said and done there is nothing in my "selected" array.
但是,一旦说完一切,我的“选定”数组中就什么也没有了。
Your callback defines a local variable named selected
, which hides the selected
variable in the outer scope. 您的回调定义了一个名为
selected
的局部变量,该变量将selected
变量隐藏在外部作用域中。 The selected
in selected[i] =
is the selected
from function (i, selected)
, not the selected
from var selected
. 的
selected
在selected[i] =
在selected
从function (i, selected)
,而不是selected
由var selected
。
Rename one of the two variables for this to work. 重命名两个变量之一以使其正常工作。
you are opening and closing array and then doing some magic without result... 您正在打开和关闭数组,然后做一些魔术而没有结果...
it's late, but var selected = [] $('#SelectBoxContainer .DDLs :selected').each <..code....> has no result really. 已经晚了,但是var selected = [] $('#SelectBoxContainer .DDLs:selected')。每个<.. code ....>都没有结果。
try making array and then: selected.put($('#SelectBoxContainer )) 尝试制作数组,然后:selected.put($('#SelectBoxContainer))
in other words, you don't have ';' 换句话说,您没有';' after 'var selected = []'
在'var selected = []'之后
How do you expect to work with two variables named "selected" at the same time? 您希望如何同时使用两个名为“ selected”的变量? Change the name of your function's second parameter so that it doesn't shadow the array you're trying to write to:
更改函数的第二个参数的名称,以使其不会遮盖您要写入的数组:
var selected = [];
$('#SelectBoxContainer .DDLs :selected').each(function (i, item)
{
selected[i] = $(item).val();
});
You can also use the array push
method instead of bothering with indices: 您也可以使用数组
push
方法,而不用烦恼索引:
selected.push( $(item).val() )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.