jQuery多维对象数组未定义

时间:2012-09-20 00:26:59

标签: jquery object multidimensional-array

我在一个wordpress网站上将一个数组从php中带入jquery。它是一个多维数组,在我转换成$ jqueryArray(取自console.log)之后看起来像这样:

2:对象{max =“10”,min =“500”,number =“2”}

3:对象{max =“15”,min =“750”,number =“3”}

4:对象{max =“8”,min =“400”,number =“4”}

5:对象{max =“12”,min =“700”,number =“5”}

1:对象{max =“10”,min =“500”,number =“1”}

代码如下:

 jQuery(function() {
    jQuery('.wpsc_select_variation').change(function(){

        var arrayFromPHP = <?php echo json_encode($alt_tables) ?>;
        var $jqueryArray = {};

            jQuery.each(arrayFromPHP, function (key, value) {
                $jqueryArray[key] = {};
                $jqueryArray[key] = value;
            });
        console.log($jqueryArray);

        // clears the div that we will type the Table Minimum order too
        jQuery('#table-details').empty();
        var $selectedName;

        // returns an integer, specific to the Table # selected
        $selectedName = jQuery(this).find(':selected').text().replace('Table ', '');

        console.log($jqueryArray.$selectedName);

        var $newDetails = 'Table minimum order: ';
        jQuery('#table-details').append( $newDetails );
    });

});

由于某种原因$ jqueryArray。$ selectedName未定义。我可以看到$ jqueryArray有5个键,编号为1到5,但即使我尝试console.log($ jqueryArray.1);我得到了不确定。我似乎无法弄清楚如何从数组中调用数字。基本上我想要

$ jqueryArray [$ selectedName] [分钟] 我在console.log中尝试过$ jqueryArray [$ selectedName]并接收undefined以及

我添加了一个jsfiddle http://jsfiddle.net/kzuyd/14/

我拿了<?php echo json_encode($alt_tables) ?>并将其添加为变量,因为php在jsfiddle中不起作用。希望它的工作原理相同..

1 个答案:

答案 0 :(得分:1)

Updated jsFiddle

jQuery(function() {
    var arrayFromPHP = '{"1 ":{"note":null,"max":" 10","min":" 500 ","number":"1 "}," 2 ":{"note":null,"max":" 10","min":" 500 ","number":" 2 "}," 3 ":{"note":null,"max":" 15","min":" 750 ","number":" 3 "}," 4 ":{"note":null,"max":" 8","min":" 400 ","number":" 4 "}," 5 ":{"note":null,"max":" 12","min":" 700 ","number":" 5 "}}';

    var $jqueryArray = {};

    // you must parse `arrayFromPHP`, it's not actually an array now
    // it's a JSON string
    jQuery.each(JSON.parse(arrayFromPHP), function(key, value) {

        // your keys have spaces at the end of them e.g. "1 "
        // trim them first
        $jqueryArray['Table' + key.trim()] = {};
        $jqueryArray['Table' + key.trim()] = value;
    });

    jQuery('.wpsc_select_variation').change(function() {

        jQuery('#table-details').empty();
        jQuery('#table-minimum').empty();
        var $selectedName;

        $selectedName = jQuery(this)
            .find("option:selected")
            .text()
            .replace(/\s/g, '');
            // to replace all spaces, use the regex shown

        var $newDetails = 'The selected name: ' + $selectedName + '';
        jQuery('#table-details').append($newDetails);

        // use obj["min"] to get the minimum
        var $tableMin = '<br /><br />The table minimum is: ' +
                        $jqueryArray[$selectedName]["min"] + '';
        jQuery('#table-minimum').append($tableMin);
    });

});​