我该如何修复这个数组表?

时间:2013-05-08 13:03:05

标签: javascript html arrays variables

我使用由用户定义的3个值编写了一个相当简单的数组表。这3个值确定变量的值。我以前写过类似的代码,但出于某种原因,我无法弄清楚为什么这个代码不起作用。桌子很大所以我很抱歉......

var ssRidgeQty=document.getElementById("ssRidgeTrim").value;

var ssRidgeWidth=document.getElementById("ssRidgeWidth").value;

var ssRidgeColorUp;
if (ssColorChoice=="galvalume")
{
    ssRidgeColorUp="1";
}
else if (ssColorChoice=="Dark Gray" || ssColorChoice=="Antique Bronze" ||     ssColorChoice=="Rural Red" || ssColorChoice=="Dark Green" || ssColorChoice=="Fern Green")
{
    ssRidgeColorUp="2";
}
else if (ssColorChoice=="metallic copper")
{
    ssRidgeColorUp="3";
}
else if (ssColorChoice=="none" && ssRidgeQty>0)
{
    ssRidgeColorUp=0;
    alert ("please choose a ss color");
}
else
{
    ssRidgeColorUp="4";
}

var ssRidgeCap = new Array();
//retail, galvalume
ssRidgeCap [[0],[1],[14]] = 21.75;
ssRidgeCap [[0],[1],[16]] = 28.1;
ssRidgeCap [[0],[1],[18]] = 31.3;
ssRidgeCap [[0],[1],[20]] = 34.5;
ssRidgeCap [[0],[1],[22]] = 37.7;
ssRidgeCap [[0],[1],[24]] = 40.9;
ssRidgeCap [[0],[1],[26]] = 44.1;
ssRidgeCap [[0],[1],[28]] = 47.3;
ssRidgeCap [[0],[1],[30]] = 50.5;
ssRidgeCap [[0],[1],[32]] = 53.7;
ssRidgeCap [[0],[1],[34]] = 56.9;
ssRidgeCap [[0],[1],[36]] = 60.1;
ssRidgeCap [[0],[1],[38]] = 63.3;
ssRidgeCap [[0],[1],[40]] = 66.5;
//retail, Kynar
ssRidgeCap [[0],[2],[14]] = 25.85;
ssRidgeCap [[0],[2],[16]] = 31.3;
ssRidgeCap [[0],[2],[18]] = 34.9;
ssRidgeCap [[0],[2],[20]] = 38.5;
ssRidgeCap [[0],[2],[22]] = 42.1;
ssRidgeCap [[0],[2],[24]] = 45.7;
ssRidgeCap [[0],[2],[26]] = 49.3;
ssRidgeCap [[0],[2],[28]] = 52.9;
ssRidgeCap [[0],[2],[30]] = 56.5;
ssRidgeCap [[0],[2],[32]] = 60.1;
ssRidgeCap [[0],[2],[34]] = 63.7;
ssRidgeCap [[0],[2],[36]] = 67.3;
ssRidgeCap [[0],[2],[38]] = 70.9;
ssRidgeCap [[0],[2],[40]] = 74.5;
//retail, Metallic Copper
ssRidgeCap [[0],[3],[14]] = 26.55;
ssRidgeCap [[0],[3],[16]] = 35.3;
ssRidgeCap [[0],[3],[18]] = 39.4;
ssRidgeCap [[0],[3],[20]] = 43.5;
ssRidgeCap [[0],[3],[22]] = 47.6;
ssRidgeCap [[0],[3],[24]] = 51.7;
ssRidgeCap [[0],[3],[26]] = 55.8;
ssRidgeCap [[0],[3],[28]] = 59.9;
ssRidgeCap [[0],[3],[30]] = 64;
ssRidgeCap [[0],[3],[32]] = 68.1;
ssRidgeCap [[0],[3],[34]] = 72.2;
ssRidgeCap [[0],[3],[36]] = 76.3;
ssRidgeCap [[0],[3],[38]] = 80.4;
ssRidgeCap [[0],[3],[40]] = 84.5;
//retail, Syl-Poly
ssRidgeCap [[0],[4],[14]] = 22.5;
ssRidgeCap [[0],[4],[16]] = 28.9;
ssRidgeCap [[0],[4],[18]] = 32.2;
ssRidgeCap [[0],[4],[20]] = 35.5;
ssRidgeCap [[0],[4],[22]] = 38.8;
ssRidgeCap [[0],[4],[24]] = 42.1;
ssRidgeCap [[0],[4],[26]] = 45.4;
ssRidgeCap [[0],[4],[28]] = 48.7;
ssRidgeCap [[0],[4],[30]] = 52;
ssRidgeCap [[0],[4],[32]] = 55.3;
ssRidgeCap [[0],[4],[34]] = 58.6;
ssRidgeCap [[0],[4],[36]] = 61.9;
ssRidgeCap [[0],[4],[38]] = 65.2;
ssRidgeCap [[0],[4],[40]] = 68.5;
//Contractor, galvalume
ssRidgeCap [[1],[1],[14]] = 20.38;
ssRidgeCap [[1],[1],[16]] = 26.5;
ssRidgeCap [[1],[1],[18]] = 29.5;
ssRidgeCap [[1],[1],[20]] = 32.5;
ssRidgeCap [[1],[1],[22]] = 35.5;
ssRidgeCap [[1],[1],[24]] = 38.5;
ssRidgeCap [[1],[1],[26]] = 41.5;
ssRidgeCap [[1],[1],[28]] = 44.5;
ssRidgeCap [[1],[1],[30]] = 47.5;
ssRidgeCap [[1],[1],[32]] = 50.5;
ssRidgeCap [[1],[1],[34]] = 53.5;
ssRidgeCap [[1],[1],[36]] = 56.5;
ssRidgeCap [[1],[1],[38]] = 59.5;
ssRidgeCap [[1],[1],[40]] = 62.5;
//Contractor, Kynar
ssRidgeCap [[1],[2],[14]] = 25.2;
ssRidgeCap [[1],[2],[16]] = 30.5;
ssRidgeCap [[1],[2],[18]] = 34;
ssRidgeCap [[1],[2],[20]] = 37.5;
ssRidgeCap [[1],[2],[22]] = 41;
ssRidgeCap [[1],[2],[24]] = 44.5;
ssRidgeCap [[1],[2],[26]] = 48;
ssRidgeCap [[1],[2],[28]] = 51.5;
ssRidgeCap [[1],[2],[30]] = 55;
ssRidgeCap [[1],[2],[32]] = 58.5;
ssRidgeCap [[1],[2],[34]] = 62;
ssRidgeCap [[1],[2],[36]] = 65.5;
ssRidgeCap [[1],[2],[38]] = 69;
ssRidgeCap [[1],[2],[40]] = 72.5;
//Contractor, Meta1llic Copper
ssRidgeCap [[1],[3],[14]] = 25.85;
ssRidgeCap [[1],[3],[16]] = 34.5;
ssRidgeCap [[1],[3],[18]] = 38.5;
ssRidgeCap [[1],[3],[20]] = 42.5;
ssRidgeCap [[1],[3],[22]] = 46.5;
ssRidgeCap [[1],[3],[24]] = 50.5;
ssRidgeCap [[1],[3],[26]] = 54.5;
ssRidgeCap [[1],[3],[28]] = 58.5;
ssRidgeCap [[1],[3],[30]] = 62.5;
ssRidgeCap [[1],[3],[32]] = 66.5;
ssRidgeCap [[1],[3],[34]] = 70.5;
ssRidgeCap [[1],[3],[36]] = 74.5;
ssRidgeCap [[1],[3],[38]] = 78.5;
ssRidgeCap [[1],[3],[40]] = 82.5;
//Contractor, Syl-Poly
ssRidgeCap [[1],[4],[14]] = 21.75;
ssRidgeCap [[1],[4],[16]] = 28.1;
ssRidgeCap [[1],[4],[18]] = 31.3;
ssRidgeCap [[1],[4],[20]] = 34.5;
ssRidgeCap [[1],[4],[22]] = 37.7;
ssRidgeCap [[1],[4],[24]] = 40.9;
ssRidgeCap [[1],[4],[26]] = 44.1;
ssRidgeCap [[1],[4],[28]] = 47.3;
ssRidgeCap [[1],[4],[30]] = 50.5;
ssRidgeCap [[1],[4],[32]] = 53.7;
ssRidgeCap [[1],[4],[34]] = 56.9;
ssRidgeCap [[1],[4],[36]] = 60.1;
ssRidgeCap [[1],[4],[38]] = 63.3;
ssRidgeCap [[1],[4],[40]] = 66.5;
//Wholesale, galvalume
ssRidgeCap [[2],[1],[14]] = 19;
ssRidgeCap [[2],[1],[16]] = 24.9;
ssRidgeCap [[2],[1],[18]] = 27.7;
ssRidgeCap [[2],[1],[20]] = 30.5;
ssRidgeCap [[2],[1],[22]] = 33.3;
ssRidgeCap [[2],[1],[24]] = 36.1;
ssRidgeCap [[2],[1],[26]] = 38.9;
ssRidgeCap [[2],[1],[28]] = 41.7;
ssRidgeCap [[2],[1],[30]] = 44.5;
ssRidgeCap [[2],[1],[32]] = 47.3;
ssRidgeCap [[2],[1],[34]] = 50.1;
ssRidgeCap [[2],[1],[36]] = 52.9;
ssRidgeCap [[2],[1],[38]] = 55.7;
ssRidgeCap [[2],[1],[40]] = 58.5;
//Wholesale, Kynar
ssRidgeCap [[2],[2],[14]] = 22.48;
ssRidgeCap [[2],[2],[16]] = 26.5;
ssRidgeCap [[2],[2],[18]] = 29.5;
ssRidgeCap [[2],[2],[20]] = 32.5;
ssRidgeCap [[2],[2],[22]] = 35.5;
ssRidgeCap [[2],[2],[24]] = 38.5;
ssRidgeCap [[2],[2],[26]] = 41.5;
ssRidgeCap [[2],[2],[28]] = 44.5;
ssRidgeCap [[2],[2],[30]] = 47.5;
ssRidgeCap [[2],[2],[32]] = 50.5;
ssRidgeCap [[2],[2],[34]] = 53.5;
ssRidgeCap [[2],[2],[36]] = 56.5;
ssRidgeCap [[2],[2],[38]] = 59.5;
ssRidgeCap [[2],[2],[40]] = 62.5;
//Wholesale, Meta1llic Copper
    ssRidgeCap [[2],[3],[14]] = 23.15;
ssRidgeCap [[2],[3],[16]] = 30.5;
ssRidgeCap [[2],[3],[18]] = 34;
ssRidgeCap [[2],[3],[20]] = 37.5;
ssRidgeCap [[2],[3],[22]] = 41;
ssRidgeCap [[2],[3],[24]] = 44.5;
ssRidgeCap [[2],[3],[26]] = 48;
ssRidgeCap [[2],[3],[28]] = 51.5;
ssRidgeCap [[2],[3],[30]] = 55;
ssRidgeCap [[2],[3],[32]] = 58.5;
ssRidgeCap [[2],[3],[34]] = 62;
ssRidgeCap [[2],[3],[36]] = 65.5;
ssRidgeCap [[2],[3],[38]] = 69;
ssRidgeCap [[2],[3],[40]] = 72.5;
//Wholesale, Syl-Poly
    ssRidgeCap [[2],[4],[14]] = 19.1;
ssRidgeCap [[2],[4],[16]] = 26.5;
ssRidgeCap [[2],[4],[18]] = 29.5;
ssRidgeCap [[2],[4],[20]] = 32.5;
ssRidgeCap [[2],[4],[22]] = 35.5;
ssRidgeCap [[2],[4],[24]] = 38.5;
ssRidgeCap [[2],[4],[26]] = 41.5;
ssRidgeCap [[2],[4],[28]] = 44.5;
ssRidgeCap [[2],[4],[30]] = 47.5;
ssRidgeCap [[2],[4],[32]] = 50.5;
ssRidgeCap [[2],[4],[34]] = 53.5;
ssRidgeCap [[2],[4],[36]] = 56.5;
ssRidgeCap [[2],[4],[38]] = 59.5;
ssRidgeCap [[2],[4],[40]] = 62.5;    

var ssRidgePrice = ssRidgeCap [[priceLevel],[ssRidgeColorUp],[ssRidgeWidth]];


document.getElementById("ssRidgeTest4").innerHTML=ssRidgePrice;

结果始终来自标有Wholesale syl-pol的最后一组数组。如果我改变宽度,那么值会改变,但如果我改变价格水平或颜色(数组中的前2 [],则没有任何变化。由于某些原因,这些值被卡在[2]和[4]上。

如果您需要任何其他参考,请询问,我会发布。我需要紧急帮助

(ps。价格水平由标记为1,2,3的单选按钮设置,当我进行测试以打印出三个决定变量“priceLevel”,“ssRidgeColorUp”和ssRidgeWidth时,它们总是正确但是数组没有把它们拿起来?

提前感谢您的时间。

3 个答案:

答案 0 :(得分:1)

而不是数组使用对象

var ssRidgeCap = {
    pl_0 : {
        rc_1 : {
            rw_14 : 21.75,
            rw_16 : 28.1
        },
        rc_2 : {
            rw_14 : 25.85,
            rw_16 : 31.3
        }
    },
    pl_1 : {
        rc_1 : {
            rw_14 : 20.38,
            rw_16 : 26.5
        },
        rc_2 : {
            rw_14 : 25.2,
            rw_16 : 30.5
        }
    }
}

然后

var ssRidgePrice = ssRidgeCap['pl_' + priceLevel]['rc_' + ssRidgeColorUp]['rw_' + ssRidgeWidth];

答案 1 :(得分:1)

另一种解决方案是使用多维数组

var ssRidgeCap = new Array();

ssRidgeCap[0] = [];
ssRidgeCap[0][1] = [];
ssRidgeCap[0][1][14]= 21.75;

然后可以使用

进行访问
ssRidgeCap[0][1][14]

答案 2 :(得分:0)

为什么不在一个命令中定义多维数组?

访问一个multidim数组元素(比如2 dim)的语法是

a[i][j] = X;

当您访问已定义数组的不存在元素时,如果不存在,则创建所有前面的元素。

当您访问非现有数组的(非)现有元素时,将发生错误。

var a = [];
a[5] = 4; // ok, elements 0,1,2,3,4 are created and set to undefined
a[6][4] = 53; // not ok, error, cannot convert undefined to object.

示例定义

var ssRidgeCap = 
[
   // 0
  [
     // 0, 0
     [ 54, 35, 64 ],
     // 0, 1
     [ 54, 23, 66 ]
   ],
   // 1
      // 1, 0
      [ 23, 6, 75],
      // 1, 1
      [ 22,  66, 22]
   ]
];