php 为什么我的数组中的某些值未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19807953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are some values in my array undefined
提问by Xtreme
I have a for loop in php that adds a number of checkboxes on my page that look like this
我在 php 中有一个 for 循环,它在我的页面上添加了许多看起来像这样的复选框
<input type="checkbox" name="checkbox[]">
I want to use javascript to check which is checked and add value in an Array
我想使用 javascript 来检查哪个被检查并在数组中添加值
var cboxes = document.getElementsByName('checkbox[]');
var len = cboxes.length;
var imageArray = new Array();
for (var i = 0; i < len; i++) {
if (cboxes[i].checked) {
imageArray[i] = cboxes[i].value;
}
}
If I have 50 boxes and click the checkbox number 2,4 and 6, loops through my array, I get the result.
如果我有 50 个框并单击复选框编号 2,4 和 6,循环遍历我的数组,我会得到结果。
for(var i = 0; i < imageArray.length; i++){
gallery.innerHTML += imageArray[i] + "<br>";
}
--
——
undefined
Correct value
undefined
Correct value
undefined
Correct value
If I check number 1, 2, 3 I get the result
如果我检查数字 1、2、3,我会得到结果
Correct value
Correct value
Correct value
Why do I get undefined when I skip a checkbox? How do I fix it
为什么跳过复选框时会出现未定义?我如何解决它
回答by lonesomeday
This is because you are adding extra elements to an array. Take this code, for instance:
这是因为您要向数组添加额外的元素。以这段代码为例:
var a = []; // empty array
a[1] = 'foo'; // don't set a[0]
console.log(a.length); // gives 2
Javascript will always "fill in" gaps in the list of array keys. If you miss some out, Javascript will fill in the blanks with undefined
.
Javascript 将始终“填充”数组键列表中的空白。如果您错过了一些,Javascript 将用undefined
.
Rather than adding the element to your array by the key name, just push
it onto the end of the array:
不是通过键名将元素添加到数组中,而是将其添加到数组push
的末尾:
imageArray.push(cboxes[i].value);
回答by Anthony Grist
You get undefined because you're skipping indexes in imageArray
. If the first checkbox isn't checked, it won't put anything in index 0, then because the second checkbox is checked, the first entry is placed into index 1.
你得到 undefined 因为你跳过了imageArray
. 如果第一个复选框没有被选中,它不会在索引 0 中放置任何东西,然后因为第二个复选框被选中,第一个条目被放入索引 1。
When you iterate it doesn't skip those missed indexes if there isan index with a value after it, they just don't have a set value so it comes back as undefined
.
当你迭代它不会跳过那些错过的指标,如果有是用后一个值的索引,他们只是没有一个设定值,所以它回来的undefined
。
You could change this line:
你可以改变这一行:
imageArray[i] = cboxes[i].value;
to:
到:
imageArray.push(cboxes[i].value);
That way it won'tskip indexes when there are unchecked checkboxes.
这样,当有未选中的复选框时,它不会跳过索引。
回答by James Donnelly
It's because you're setting the value of imageArray[i]
onlywhen the corresponding checkbox is checked. If you check checkboxes 2, 4 and 6, you're essentially doing this:
这是因为您imageArray[i]
仅在选中相应复选框时才设置 的值。如果您选中复选框 2、4 和 6,您实际上是在执行以下操作:
imageArray[1] = cboxes[1].value;
imageArray[3] = cboxes[3].value;
imageArray[5] = cboxes[5].value;
imageArray[0]
, [2]
and [4]
are never being set, and thus are undefined
.
imageArray[0]
,[2]
并且[4]
永远不会被设置,因此是undefined
。
To fix this, either make use of push()
to push the values into the imageArray
, or simply set dummy values for non-matching keys:
要解决此问题,请使用push()
将值推送到 中imageArray
,或者简单地为不匹配的键设置虚拟值:
for (var i = 0; i < len; i++) {
if (cboxes[i].checked) {
imageArray[i] = cboxes[i].value;
} else {
imageArray[i] = "";
}
}
The result:
结果:
imageArray[0] = "";
imageArray[1] = cboxes[1].value;
imageArray[2] = "";
imageArray[3] = cboxes[3].value;
imageArray[4] = "";
imageArray[5] = cboxes[5].value;
Alternatively using push()
:
或者使用push()
:
for (var i = 0; i < len; i++) {
if (cboxes[i].checked) {
imageArray.push(cboxes[i].value);
}
}
The result:
结果:
imageArray[0] = cboxes[1].value;
imageArray[1] = cboxes[3].value;
imageArray[2] = cboxes[5].value;
回答by rab
try this
尝试这个
var cboxes = document.getElementsByName('checkbox[]');
var imageArray =[];
for (var i = 0, len = cboxes.length ; i < len; i++) {
if (cboxes[i].checked) {
imageArray.push(cboxes[i].value );
}
}
回答by Deepika Janiyani
You are setting the imageArray with respect to the variable i,
您正在设置关于变量 i 的 imageArray,
the loop is executing each time and hence it is setting undefined value for those elements in the array that are not set,
循环每次都在执行,因此它为数组中未设置的元素设置了未定义的值,
you should use a different loop variable, and increase its value only on successful condition.
您应该使用不同的循环变量,并且仅在成功的情况下才增加其值。
Try modifying loop as below.
尝试修改循环如下。
var j=0;
for (var i = 0; i < len; i++) {
if (cboxes[i].checked == true) {
imageArray[j] = cboxes[i].value;
j++;
}
}