0

我在 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 中不起作用。希望它的工作原理相同..

4

1 回答 1

1

更新了 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);
    });

});​
于 2012-09-20T02:04:03.063 回答