将嵌套值附加到下拉菜单

时间:2013-04-03 01:58:08

标签: php json jquery jquery-mobile

我需要访问嵌套数据并将其动态添加到下拉菜单中。 Jquery移动Web应用程序是使用场景。因此,ajax调用正在向php发送curls,然后将json返回到分解数据的Web应用程序。

这是一个对象的例子(注意:它已在php中解码,然后将被重新编码)

[rates] => Array
    (
        [0] => stdClass Object
            (
                [type] => rate
                [categoryID] => 
                [duration] => PT60M
                [cost] => 5.00
                [recordID] => PT60M|59
                [summary] => $5 for the first 60 minutes, then $5 for each additional 60 minutes
                [summaryColumns] => Array
                    (
                        [0] => stdClass Object
                            (
                                [type] => rateSummaryColumn
                                [text] => $5.00
                                [align] => right
                                [lines] => 1
                            )

                        [1] => stdClass Object
                            (
                                [type] => rateSummaryColumn
                                [text] => 60 minutes
                                [align] => right
                                [lines] => 1
                            )

                    )

            )

        [1] => stdClass Object
            (
                [type] => rate
                [categoryID] => 
                [duration] => PT120M
                [cost] => 10.00
                [recordID] => PT120M|59
                [summary] => $10 for the first 2 hours, then $5 for each additional 60 minutes
                [summaryColumns] => Array
                    (
                        [0] => stdClass Object
                            (
                                [type] => rateSummaryColumn
                                [text] => $10.00
                                [align] => right
                                [lines] => 1
                            )

                        [1] => stdClass Object
                            (
                                [type] => rateSummaryColumn
                                [text] => 2 hours
                                [align] => right
                                [lines] => 1
                            )

                    )

            )

        [2] => stdClass Object
            (
                [type] => rate
                [categoryID] => 
                [duration] => PT180M
                [cost] => 15.00
                [recordID] => PT180M|59
                [summary] => $15 for the first 3 hours, then $5 for each additional 60 minutes
                [summaryColumns] => Array
                    (
                        [0] => stdClass Object
                            (
                                [type] => rateSummaryColumn
                                [text] => $15.00
                                [align] => right
                                [lines] => 1
                            )

                        [1] => stdClass Object
                            (
                                [type] => rateSummaryColumn
                                [text] => 3 hours
                                [align] => right
                                [lines] => 1
                            )

                    )

            )

我需要访问摘要列中的文本,并将它们附加到带有[text] of rates [0] .summaryColumns [0] .text + rates [0] summaryColumns [1] .text的下拉列表中。我有一个每个语句附加每个文本作为选项但我需要将其中两个组合在一起 - 所以需要重写

 var _rate = result.rates;
$.each(_rate, function(i, vDetails) {
                        var vRate = (vDetails.summaryColumns);
                        $.each(vRate, function(i2, vHandle){
                        //console.log(vHandle.text);
                        $('#rates').append($('<option/>').attr("value", vHandle.text).text(vHandle.text));

                        });
                    });

如果可以,请提出建议 - 谢谢。

1 个答案:

答案 0 :(得分:0)

我发现这对我有用。

   var _rate = result.rates;
                        $.each(_rate, function(i) {
                        $('#rates').append($('<option/>').attr("value", _rate[i].cost).text(_rate[i].summaryColumns[0].text + _rate[i].summaryColumns[1].text));
                        });

这很有效 - 如果摘要列超出text属性下的2个值,我可能会遇到问题。感谢您的反馈。