我的带有数组的javascript脚本出了点问题

时间:2012-03-08 14:46:51

标签: javascript jquery

基本上我的javascript数组脚本看起来像 -

cars = $('#bike').val();
var priceone = document.documentElement.getAttribute('priceone');
var pricetwo = document.documentElement.getAttribute('pricetwo');
var pricethree = document.documentElement.getAttribute('pricethree');
var pricefour = document.documentElement.getAttribute('pricefour');
var pricefive = document.documentElement.getAttribute('pricefive');
var pricesix = document.documentElement.getAttribute('pricesix');
var priceseven = document.documentElement.getAttribute('priceseven');
var priceeight = document.documentElement.getAttribute('priceeight');
var pricenine = document.documentElement.getAttribute('pricenine');
var priceMatrix = {
    cars: {
        1: {
            t1: priceone,
            t2: pricetwo,
            t3: pricethree
        },
        2: {
            t1: pricefour,
            t2: pricefive,
            t3: pricesix
        },
        3: {
            t1: priceseven,
            t2: priceeight,
            t3: pricenine
        }
    }
};

也许有人有个想法,为什么priceMatrix数组不起作用?我应该如何将所有变量插入到数组中,以便它可以工作?汽车:是在脚本和价格顶部定义的可变汽车,pricetwo ... pricenine也是在顶部定义的变量。希望你能帮忙。如果可能请写,我应该如何在javascript模式下定义var cars,所以它也需要select字段值。这是脚本,所以你可以使用它 - http://jsfiddle.net/tSsvb/2/

4 个答案:

答案 0 :(得分:1)

已修复:http://jsfiddle.net/tSsvb/20/

var priceMatrix;

function bikechange(){
    cars = $('#bike option:selected');
    var priceone = cars.attr('priceone');
    var pricetwo = cars.attr('pricetwo');
    var pricethree = cars.attr('pricethree');
    var pricefour = cars.attr('pricefour');
    var pricefive = cars.attr('pricefive');
    var pricesix = cars.attr('pricesix');
    var priceseven = cars.attr('priceseven');
    var priceeight = cars.attr('priceeight');   
    var pricenine = cars.attr('pricenine');   

    cars = cars.val();
    priceMatrix = eval('(' +"{"+cars+": {1: {t1: priceone, t2: pricetwo, t3: pricethree}, 2: {t1: pricefour, t2: pricefive, t3: pricesix},3: {t1: priceseven, t2: priceeight, t3: pricenine}}}"+')');
}

然后:

 $('.recalc').change(function() {
        bikechange();  //call bikechange function here
        .
        .
        .

答案 1 :(得分:0)

可能是Price矩阵在代码中没有被定义为数组,它被定义为JSON对象。

JSON数组看起来像方括号(car是以下代码中的数组):

var priceMatrix = {
    cars: [
        {
            t1: priceone,
            t2: pricetwo,
            t3: pricethree
        },
        {
            t1: pricefour,
            t2: pricefive,
            t3: pricesix
        },
        {
            t1: priceseven,
            t2: priceeight,
            t3: pricenine
        }
    ]
};

答案 2 :(得分:-1)

priceMatrix不是数组,是一个对象,因为它使用{...}语法,而不是[...]。

使用对象,整数本身不是有效的属性名称。您需要将“1”,“2”,“3”更改为其他内容才能使其正常工作。

如果要在代码中使用此对象,可以使用以下语法之一访问其属性:

点符号

var firstCar = priceMatrix.cars.first;
// firstCar is now an object containing 3 properties, t1, t2, and t3

或括号表示法

var firstCar = priceMatrix['cars']['first'];
// firstCar is now an object containing 3 properties, t1, t2, and t3

如何与此对象进行交互的演示文稿:http://jsfiddle.net/cjDd9/

答案 3 :(得分:-1)

您的部分问题来自JavaScript中ArraysObjects之间的区别。您发布的代码中实际上没有单个数组。与priceMatrixcars1一样,2是一个对象,与3一样。 JS中的数组语法使用方括号[]而不是用于对象的卷曲{}。虽然您没有明确说出来,但您肯定会收到无效的语法错误,因为您发布的代码中的对象属性不能是数字(123)。

在某个级别上,您可以将数组视为普通的索引数组(即,其元素可以通过其索引号访问,如1,2或3),而JS对象充当关联数组(即其元素可能是通过字符串访问,如't1')。这是一个严重的过度简化,但它可能有助于理清您当前的问题。

说了这么多,你的小提琴中的代码显然存在另一个问题。如果您打开控制台,您会看到如下错误:

Uncaught TypeError: Cannot read property '1' of undefined

这就发生在这一行:

console.log(days + ' days in season ' + season + ' at ' + priceMatrix[bike][season][tier] + '/day (' + tier + ')')

如果您只记录bike变量的值,您会发现它等于bike1。因此,您尝试访问priceMatrix.bike1。但那不存在。 priceMatrix.cars确实存在,但不存在priceMatrix.bike1

无论如何,我不确定你的最终目标是什么,所以我不能说以下内容会完全解决它,但它至少会删除错误:

var priceMatrix = {
    bike1: [
        { t1: priceone, t2: pricetwo, t3: pricethree},
        { t1: pricefour, t2: pricefive, t3: pricesix},
        { t1: priceseven, t2: priceeight, t3: pricenine}
    ]    
};

我已将cars更改为bike1,因为这是console.log尝试访问的属性。我还将priceMatrix.bike1转换为数组(注意方括号[]括号而不是卷曲括号,以及省略任何属性名称(如1,{{1} },2)。

这是一个更新小提琴的链接,不会产生任何JS错误:http://jsfiddle.net/tSsvb/16/