2014-09-30 68 views
0

我有一個像下面這樣的JSON數組。Jquery解析JSON數組

[ 
    { 
     "0":1, 
     "companyID":1, 
     "1":"A", 
     "companyName":"A", 
     "2":"asd", 
     "companyAddress":"asd", 
     "3":"sad", 
     "companyWebAddress":"sad", 
     "4":"(222) 222-222", 
     "companyPhone":"(222) 222-222", 
     "5":"(222) 222-222", 
     "companyFax":"(222) 222-222", 
     "6":"1", 
     "yearOfEstablishment":"1", 
     "7":"1", 
     "fieldOfBusiness":"1", 
     "8":"Job Shop", 
     "typeOfProductionService":"Job Shop", 
     "9":"Apparel", 
     "mainProductsServices":"Apparel" 
    } 
] 

我使用JQuery + Ajax從PHP頁面獲得了它。我想分析它,並把它的值放到幾個文本框上,放在選擇框的更改函數上。這是我的嘗試,但它不工作。 任何幫助將appriciated。

$(document).ready(function(){ 
    $("#companyFillSelectBox").change(function(){ 
     var value = $("select#companyFillSelectBox option:selected").val(); 
     //alert(); 
     //$('#companyNameText').val("SHIT"); 
     $.ajax({ 
      type: 'POST', 
      url: 'companyFill.php', 
      dataType: "json", 
      data:{companyID:value}, 
      success:function(answer){ 
       $('#companyNameText').val(answer.companyName); 
      }, 
      error:function(){ 
       alert("An error has occured !"); 
      }   
     }); 
    }); 
}); 
+0

你可能不需要索引鍵以及命名鍵 – charlietfl 2014-09-30 20:13:14

回答

0

這是一個數組,所以answer[0].companyName讓你的第一個對象數組中,然後companyName財產。