【问题标题】:jQuery multidimensional object array coming up undefinedjQuery多维对象数组出现未定义
【发布时间】:2012-09-12 08:22:13
【问题描述】:

我在 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/

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

【问题讨论】:

  • 您没有将$selectedName 添加到$jqueryArray...
  • 我该怎么做? $selectedName 是一个整数,比如 1 或 2,$jqueryArray 有整数 1 到 5 作为键
  • 我试过 console.log($jqueryArray["1"]);和 console.log($jqueryArray[1]);并且都返回 undefined
  • 如果可能的话,发布一个 jsfiddle 会有所帮助。

标签: jquery object multidimensional-array


【解决方案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);
    });

});​

【讨论】:

  • 问题是因为某些变量有多余的空格吗?无论如何,谢谢你的帮助!!我花了几个小时在这个
  • 有几个问题,主要是我在上面的代码中注释的问题。如果我不得不责怪一个问题,尽管它是你的 .each() 循环,因为你的“数组”变量实际上只是一个字符串,它是 iterates over its characters instead
  • 那个问题只是在小提琴中。原始代码是 var arrayFromPHP = ;但我不能直接把它带入小提琴。很高兴您注意到并修复了它,再次感谢!
猜你喜欢
  • 2012-09-01
  • 2017-09-25
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
相关资源
最近更新 更多