2013-01-22 18 views
0

使用的insertRow()到特定的行ID

var row = table.insertRow(id); 

我怎麼可以指定新行那張用來選擇ID下,而不是硬編碼的索引或在表的末尾?我有一個下拉菜單,它具有不同的選項,用於放置新行的行ID。下拉選項與相關行具有匹配的ID。謝謝。這裏是我的表,我想新行轉到用戶選擇父親去下(father3,father4,或father5)

<table id="shore_tab" border="1"> 
         <tr class="row_blue_bold father" id="father3"> 
         <td colspan="2" class="father_header">Category 3</td> 
         <td class="cell_50">&nbsp;</td> 
         </tr> 
          <tr class="row_blue_bold son3"> 
           <td class="cell_20 cell_no_bkg">&nbsp;</td> 
           <td class="cell_190">(information for father3, category 3)</td> 
           <td class="cell_50">&nbsp;</td> 
          </tr> 
          <tr class="row_blue_bold son3"> 
           <td class="cell_20 cell_no_bkg">&nbsp;</td> 
           <td class="cell_190">(information for father3, category 3)</td> 
           <td class="cell_50">&nbsp;</td> 
          </tr> 
         <tr class="row_blue_bold father" id="father4"> 
         <td colspan="2" class="father_header">Category 4</td> 
         <td class="cell_50">&nbsp;</td> 
         </tr> 
          <tr class="row_blue_bold son4"> 
           <td class="cell_20 cell_no_bkg">&nbsp;</td> 
           <td class="cell_190">(information for father4, category 4)</td> 
           <td class="cell_50">&nbsp;</td> 
          </tr> 
          <tr class="row_blue_bold son4"> 
           <td class="cell_20 cell_no_bkg">&nbsp;</td> 
           <td class="cell_190">(information for father4, category 4)</td> 
           <td class="cell_50">&nbsp;</td> 
          </tr> 
        </table> 
+0

你可能有獲得與ID列的索引,並將它傳遞(+1),以'insertRow'。 –

回答

2

您可以使用id來獲取指數

var row = table.insertRow(document.getElementById(id).rowIndex+1);//+1 to be inserted under 
+0

我更新了我的問題。根據用戶從下拉菜單中選擇的內容,我需要將新行放在father3,father4或father5下。 – triplethreat77

+0

@ triplethreat77'id'將會是用戶選擇的id像'father3'等 – Musa

+0

這似乎是一個完美的解決方案,但我無法讓它工作。你能看看我以前的帖子嗎?下拉代碼也在那裏。 http://stackoverflow.com/questions/14463030/add-values-to-correct-father-section-of-table – triplethreat77

0

id應該index,即:table.rows就像是一個數組(它實際上不是一個數組,但行爲像一個)。

+0

var row = table.insertRow(0);喜歡這個。但我不希望它在0或-3的指標。我需要它放在選定的行ID之後。 – triplethreat77

+0

傳入行的索引(不是'id',因爲沒有這個東西)。 – Halcyon