2013-04-03 61 views
0

我需要訪問嵌套數據並將其添加到下拉菜單中動態。 JQuery的移動網絡應用程序是使用場景。所以ajax調用將捲髮到php,然後將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 
          ) 

        ) 

      ) 

我需要從彙總列訪問文本其附加在一個下拉列表與[rate]的[text] .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

什麼在一起?提供json輸出,而不是php轉儲。 – charlietfl

+0

並確保JSON有效 - PHP轉儲不是。 –

+0

我需要它來閱讀選項選擇= $ 10.00 2小時 – Ezos

回答

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)); 
         }); 

這有效 - 如果彙總列超出文本屬性下的2個值,我可能會遇到問題。感謝您的反饋。