【发布时间】: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/
我取了<?php echo json_encode($alt_tables) ?> 并将其添加为变量,因为 php 在 jsfiddle 中不起作用。希望它的工作原理相同..
【问题讨论】:
-
您没有将
$selectedName添加到$jqueryArray... -
我该怎么做? $selectedName 是一个整数,比如 1 或 2,$jqueryArray 有整数 1 到 5 作为键
-
我试过 console.log($jqueryArray["1"]);和 console.log($jqueryArray[1]);并且都返回 undefined
-
如果可能的话,发布一个 jsfiddle 会有所帮助。
标签: jquery object multidimensional-array