简体   繁体   English

我想念什么吗? 我的值不会存储到我的数组中

[英]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 . selectedselected[i] =selectedfunction (i, selected) ,而不是selectedvar 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.

相关问题 我在导出组件时丢失了什么吗? - Am I missing something with exporting my component? 我的 JavaScript 代码中是否遗漏了一些我可能会忽略的内容? - Am I missing something in my JavaScript code that I may be overlooking? 引导程序干扰了我的Jqueryui还是我缺少了什么? - Is bootstrap interfering with my Jqueryui or am i missing something? 我没有从Firebase数据库中获取值 - I don't get the values from my firebase database 我似乎无法让我的代码显示我的计算,我是否缺少 output 标签? - I can't seem to get my code to display my calculation, am I missing an output tag? 为什么我的悬停添加类在Jsfiddle上有效,但在我的网站上却不可用? 我的标题中是否缺少某些内容? - Why is my add class on hover working on Jsfiddle but not on my website? Am I missing something in my header? 为什么我的代码不过滤数组?我缺少什么? - why my code is not filtering the array?what am i missing? 为什么我的数组值不能反映我在屏幕上看到的内容? - Why don’t my array values reflect what I see on the screen? 默认情况下,我的节点服务器上似乎启用了Cors。 我想念什么吗? - Cors seems to be enabled on my node server by default. Am I missing something? 所以我试图在一个数组中获取我的 axios 响应,但我不知道该怎么做 - So i'm trying to get my axios response in an array and I don't know how to do it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM