简体   繁体   English

javascript:全部数组 <select>项目返回未定义。为什么?

[英]javascript: array of all <select> items comes back undefined. why?

i have a series of <input> and <select> items intermingled. 我混合了一系列<input><select>项目。 i want to populate an array with these items as they appear on the page. 我想用这些项目在页面上显示的数组来填充它们。

i created the inputItemsArray and the selectItemsArray the same way: 我以相同的方式创建了inputItemsArrayselectItemsArray

var inputItemsArray = document.getElementsById("formGroup").getElementsByTag("input");
var inputSelectArray = document.getElementsById("formGroup").getElementsByTag("select");

the input array works out just fine. input数组工作得很好。 the select array returns undefined. select数组返回未定义。 why? 为什么?

what i'm trying to do is splice the contents of the submit array into the input array and THEN dump the values of those elements into a final array. 我想做的是将submit数组的内容拼接到input数组中,然后将这些元素的值转储到最终数组中。

it works fine if i just use the input array, but because the select array is undefined... 如果我只是使用input数组,它会很好地工作,但是因为select数组是未定义的...

here's the code: 这是代码:

var clientInputArray = document.getElementById("clientData").getElementsByTagName("input");
var clientSelectAray = document.getElementById("clientData").getElementsByTagName("select");

clientInputArray.splice(4,0,clientSelectArray[0]);  //  insert townType into array
clientInputArray.splice(5,0,clientSelectArray[1]);  //  insert province into array

console.log("clientInputArray: " + clientInputArray);

for (data in clientArray)
{
    if (clientArray[data].length != 0)
    {
        clientData.push(clientArray[data].value);
        console.log("data: " + data);
    } else {    //  because the last two elements of the array are empty...don't know why...
            break;
    }
}   
console.log("clientData: " + clientData);   
//clientData.push(companyName);

so, to restate: why does the select version return undefined? 因此,请重申一下:为什么select版本返回未定义?

WR! WR!

var inputItemsArray = document.getElementsById("formGroup").getElementsByTag("input");
var inputSelectArray = document.getElementsById("**formsGroup**").getElementsByTag("select");

You have an 's' that should not be there in formsGroup... This is why you are getting undefined. 您在FormsGroup中不应该有一个's'。这就是为什么您未定义的原因。

Your problem is: 您的问题是:

  1. You are treating the result of getElementsByTagName as an Array, but it's actually a NodeList . 您将getElementsByTagName的结果视为一个数组,但实际上是一个NodeList
  2. You are using lazy for (a in b) iteration of the results instead of a proper numeric for loop. 您正在for (a in b)迭代使用惰性for (a in b)而不是使用适当的数字for循环。

Here is an updated and simplified test: http://jsfiddle.net/MMXKW/2/ 这是更新和简化的测试: http : //jsfiddle.net/MMXKW/2/

As you can see: 如你看到的:

  • The results of both of them have non-zero length. 两者的结果都具有非零长度。
  • When you iterate using for (a in b) on a NodeList you also get the enumerable properties of length and item . 当在NodeList上使用for (a in b)进行迭代时for (a in b)您还将获得lengthitem的可枚举属性。

Instead, use: 而是使用:

var selects = myForm.getElementsByTagName('select');
for (var i=0,len=selects.length;i<len;++i){
  var select = selects[i];
  // ...
}

As a side note, when you write for (foo in bar) you are also creating a global foo variable; 附带说明一下,当您写for (foo in bar)您还创建了一个全局foo变量。 you should normally use for (var foo in bar) when you actually need to enumerate properties of an object and not numeric values of an Array or Array-like object. 当您实际上需要枚举对象的属性而不是枚举Array或类似Array的对象的数值时,通常应该使用for (var foo in bar)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM