2010-10-23 61 views
2

這可能是令人難以置信的基礎。在下面的代碼中,我註釋了需要注意的部分。我只是不知道如何構造它,而不是res.DATA.VALUE [i],我使它動態並寫入'res.DATA'。 + textCol + '[我]'(純僞代碼,我意識到它不會工作)動態訪問JSON值

function loadSelect(entity,textCol,retField,method) { 
     var thisid; 
     var thisval;  
     var textCol = textCol.toUpperCase(); 

     $.getJSON("/cfcs/system.cfc?method=" + method + "&returnformat=json",{},function(res,code) { 
      if(res && res.ROWCOUNT > 0) 
      { 
       for(var i=0; i<res.ROWCOUNT; i++){ 
        thisid = parseInt(res.DATA.RECORD_ID[i]); 
        thisval = res.DATA.VALUE[i]; //instead of VALUE, I want to use the textCol argument passed to this function. 

..../snip 

回答

4

您可以使用bracket notation通過字符串名來訪問屬性,像這樣:

thisval = res.DATA[textCol][i]; 
+1

甜, 朋友,謝謝 – Paul 2010-10-23 00:39:28