我在 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][min] 我在 console.log 中尝试了 $jqueryArray[$selectedName] 并且也收到了 undefined
我添加了一个 jsfiddle http://jsfiddle.net/kzuyd/14/
我把<?php echo json_encode($alt_tables) ?>
它添加为变量,因为 php 在 jsfiddle 中不起作用。希望它的工作原理相同..