2013-10-13 122 views
2

閱讀對象我有一個formpanel,有一個文本框如何ExtJS的4

  { 
      xtype: 'textfield', 
      name: name, 
      listeners: { 
       change: function(field, newValue, oldValue, eOpts){ 
        alert(newValue) // [object Object],[object Object],[object Object] 
       } 
      } 
     } 

我用form.load({...});裝載值textfield

這裏是我的JSON

{ 
"success":true, 
    "data":{ 
      "name":[ 
       {"dis":3,"val":0}, 
       {"dis":2,"val":1}, 
       {"dis":1,"val":2} 
      ] 
    } 
} 

哪有我在change函數中讀取disval。我alert(newValue)看起來像 [object Object],[object Object],[object Object]

編輯

  1. 但我儘量alert(newValue[0]);[
  2. 我打印像alert(typeof newValue);結果newValue類型爲string,我嘗試將其轉換成JSON像

var json = eval("(" + newValue + ")");

,但我得到的錯誤

SyntaxError: missing ] after element list 
([object Object],[object Object],[object Object]) 
+0

後不要使用調試警報(...)。這是非常低效的。改用console.log(newValue)。這會將變量打印到您的調試控制檯,PLUS具有能夠打印對象的全部內容的好處,而不僅僅是簡單的值。 – existdissolve

+0

@existdissolve我使用它,但仍然得到'[對象對象],[對象對象],[對象對象]':( – LookAtMeNow

+0

你使用哪個瀏覽器? – existdissolve

回答

0

我找到了解決方案。我的JSON必須看起來像

{ 
"success":true, 
    "data":{ 
      "name":[ 
       {\"dis\":3,\"val\":0}, 
       {\"dis\":2,\"val\":1}, 
       {\"dis\":1,\"val\":2} 
      ] 
    } 
} 

而且使用Ext.decode(newValue); :)

0

您可以將JSON字符串解析轉換成JavaScript對象使用以下內容

var json = Ext.decode(jsonString);

然後你就可以訪問成員,例如:

console.log(json.success);