我從一個WordPress的網站上將一個數組引入到jQuery中。它是一個多維陣列看起來像這樣後我轉換成$ jqueryArray(來自的console.log截取)jQuery多維對象數組未來定義
2:對象{最大= 「10" ,最小=」 500 「數=」 2「}
3:Object {max =「15」,min =「750」,number =「3」}
4:Object {max =「8」,min =「400」,number =「4」}
5:對象{最大= 「12" ,最小=」 700 「數=」 10 「}
1:對象{最大=」 10" ,最小= 「500」,數=「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] [分鐘] 我試過$ jqueryArray [$ selectedName]中的console.log和接收不確定以及
我加了的jsfiddle http://jsfiddle.net/kzuyd/14/
我拿了<?php echo json_encode($alt_tables) ?>
,並將其作爲變量添加,因爲php在jsfiddle中不起作用。希望它的工作原理相同..
你不加'$ selectedName'到'$ jqueryArray' ... – elclanrs
我會怎麼做呢? $ selectedName是一個整數,像1或2,$ jqueryArray有整數1到5作爲鍵 –
我試過console.log($ jqueryArray [「1」]);和console.log($ jqueryArray [1]);並且都返回undefined –