2012-07-05 56 views
0

我已經寫了一些代碼,在一個循環中,調用了一個函數,它將該循環迭代中的當前對象傳遞給該函數。訪問動態對象:javascript/jquery

問題是我不知道如何使用該對象,因爲它的動態和我只能訪問obj參數。

的Javascript

objectifyTableRow(val, i); // Populate object with properties and values 

的Val是被傳遞給函數objectifyTableRow

function objectifyTableRow(objRow, count) { // Create objects for calculations 

    var ii = 0; 
    $('#ctl00_PageContent_freight_rate_column_chaair_r' + count + " " + 'td').each(function(i, val) { // begin each 
     /* Concatenate column name with subcolumn name. example objFobCost.China/Sea Origin,Air */ 
     if (i < 3) { // columns 0,1,2 in table row 
      if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns 
       var PropertyName = arrayColumns[0] + arraySubColumn[ii]; 


       objRow[PropertyName] = parseFloat($(val).html()); // Set key name with var PropertyName 

       ii += 1; 
      } 

      if (ii == 3) { // Reset counter 
       ii = 0; 
      } 
     } // end of outer if 

     else if (i > 2 & i < 6) { 
      if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns 
       var PropertyName = arrayColumns[1] + arraySubColumn[ii]; 

       objRow[PropertyName] = parseFloat($(val).html()); 

       ii += 1; 
      } 

      if (ii == 3) { // Reset counter 
       ii = 0; 
      } 
     } // end of outer if 

     else if (i > 5 & i < 9) { 
      if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns 
       var PropertyName = arrayColumns[2] + arraySubColumn[ii]; 

       objRow[PropertyName] = parseFloat($(val).html()); 

       ii += 1; 
      } 

      if (ii == 3) { // Reset counter 
       ii = 0; 
      } 
     } // end of outer if 

     else if (i > 8 & i < 12) { 
      if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns 
       var PropertyName = arrayColumns[3] + arraySubColumn[ii]; 

       ii += 1; 
      } 

      if (ii == 3) { // Reset counter 
       ii = 0; 
      } 
     } // end of outer if 

     else { 
      if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns 
       var PropertyName = arrayColumns[4] + arraySubColumn[ii]; 

          ii += 1; 
      } 

      if (ii == 3) { // Reset counter 
       ii = 0; 
      } 
     } // end of else 
    });     // end of each loop TD 
    beginCalc(objRow); 
}; 

每個對象被傳遞給beginCalc以便計算可基於ID製成,鍵的對象和值

function beginCalc(obj) { 
    // Every obj used is passed to here 
    $.each(obj, function(key, element) { 
    alert('ID: ' + obj[this.id] + '\n' + 'key: ' + key + '\n' + 'value: ' + element); // Check correct obj id, key and value 
}); 

我這樣做的原因是因爲對象正在存儲來自asp.net網格的值,我認爲它會更清晰地爲每個網格行創建對象,然後通過選擇obj.key來完成代碼中的計算:value * obj.key:value。而不是使用getDocumentByElementId。

任何想法如何以編程方式通過它們在beginCalc函數中的ID訪問這些對象?

感謝

+0

你是什麼意思的「對象參數」?你有對象鍵和值,你在說什麼參數? – Cecchi 2012-07-05 13:48:39

+0

當我調用beginCalc()時,我提供了一個對象作爲它的參數。由於此函數調用發生在一個循環內,我正在傳遞每個對象直到循環結束。所以在beginCalc()我有多個對象具有不同的ID。每個obj也有其自己的關鍵和價值配對。這是否更有意義? – 2012-07-05 13:51:14

+0

我明白,但你在找什麼?你想在'beginCalc()'中使用你傳遞的所有對象的數組嗎?在這種情況下,您需要首先創建完整的數組,然後將其保存爲變量,然後遍歷該數據......無法神奇地獲取尚未創建的對象列表。也許我仍然是誤會。 – Cecchi 2012-07-05 13:53:39

回答

0

你想要的是DOM節點和值之間的連接。試試這個辦法(僞):

var nodes = $(...select all the nodes...); 
nodes.each(function(index, node) { 
    var obj = { node: node }; 
    beginCalc(obj); 
}); 

beginCalc(),你現在可以用obj.node訪問DOM節點。您可以使用相同的方法將更多數據放入對象中,以將所有內容保存在一個位置(需要更新的DOM節點+需要更新的所有數據)。

+0

當你說「$(... ...選擇所有節點...」);「我不確定你的意思? – 2012-07-05 14:22:43

+0

我的意思是'$('#ctl00_PageContent_freight_rate_column_chaair_r'+ count +「」+'td')'。重點是你應該爲每個節點創建一個'obj',而不是將'PropertyName'下的所有節點值保存在一個對象中。 – 2012-07-05 15:47:04