简体   繁体   English

创建动态数组Javascript

[英]creating dynamic Arrays Javascript

Hello i want to create a array in java script withing 2 for loops 您好我想在java脚本中创建一个带有2个for循环的数组

var i;
    var a;
    var total       = document.getElementsByName('qm[7]')   
    var creativity  = document.getElementsByName('qm[0]');
    var design      = document.getElementsByName('qm[1]');
    var text        = document.getElementsByName('qm[3]');
    var motivation  = document.getElementsByName('qm[5]');
    var depth       = document.getElementsByName('qm[6]');
    var usefulness  = document.getElementsByName('qm[8]');
    var research    = document.getElementsByName('qm[9]');

ratingArray = new Array(total,creativity,design,text,motivation,depth,usefulness,research);

for(i=0; i < ratingArray.length;i++)
{

    for(a=0; a < ratingArray[i].length;a++)
    {
        if(ratingArray[i][a].checked == true)
        {

             rateArray = new Array(ratingArray[i][a].value);
        }    
    }

}

and if i return rateArray it just gives the first element any idea? 如果我返回rateArray它只是给第一个元素任何想法?

You're overwriting rateArray each time you find a checked element - I suspect you meant to append it instead: 每次找到一个已检查的元素时,你都会覆盖rateArray - 我怀疑你是想要追加它:

var ratingArray = new Array(total,creativity,design,text,motivation,depth,usefulness,research);
var rateArray = new Array();

for(i=0; i < ratingArray.length;i++)
{

        for(a=0; a < ratingArray[i].length;a++)
        {
                if(ratingArray[i][a].checked == true)
                {

                         rateArray.push(ratingArray[i][a].value);
                }        
        }

}

Create a new array and push the selected values to the new array. 创建一个新数组并将所选值推送到新数组。

A detailed description of array functions 数组函数的详细说明

Manipulating JavaScript Arrays 操纵JavaScript数组

var ratingArray = new Array(total,creativity,design,text,motivation,depth,usefulness,research);

var selectedValArray = [];

for(i=0; i < ratingArray.length;i++)
{

        for(a=0; a < ratingArray[i].length;a++)
        {
                if(ratingArray[i][a].checked == true)
                {

                         selectedValArray.push ( ratingArray[i][a].value );
                }        
        }

}

The statement 该声明

document.getElementsByName('qm[7]')

will not work. 不管用。 There are no elements that can have the name qm[7] . 没有可以使用名称qm[7]元素。 Did you mean this to be your array? 你的意思是你的阵列吗? In that case, remove the quotes, initialize the array prior to those statements and fill it with the names of the elements you want to select. 在这种情况下,删除引号,在这些语句之前初始化数组,并使用要选择的元素的名称填充它。

The function getElementsByName returns an array of elements. 函数getElementsByName返回一个元素数组。 To use this array, you need to select the items in it. 要使用此数组,您需要选择其中的项目。 Ie: 即:

var elems = document.getElementsByName("body");
var myBody = elems[0];

you do that correctly in your for-loops. 你在for循环中正确地做到了这一点。

Update: expanded section and added explanation on getElementsByTagName 更新:扩展部分并在getElementsByTagName上添加说明

In this line you create an new Array every time: 在这一行中,您每次都会创建一个新数组:

rateArray = new Array(ratingArray[i][a].value);

So you have to push the elements in to the array instead of creating a new one every time thats also delete the last version. 所以你必须将元素推入数组,而不是每次都删除最后一个版本时创建一个新元素。

 var rateArray =[]

for(i=0; i < ratingArray.length;i++)
{

        for(a=0; a < ratingArray[i].length;a++)
        {
                if(ratingArray[i][a].checked)
                {

                         rateArray.push(ratingArray[i][a].value);
                }        
        }

}

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

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