2012-09-13 12 views
1

有一個鏈接被點擊時,增加了一個新行的表:添加行的一部分,作爲新行表

<table> 
       <thead> 
        <tr> 
        <th scope="col">col1</th> 
        <th scope="col">col2</th> 
        <th scope="col">col3</th> 
        </tr> 
       </thead> 

       <tbody> 
        <tr> 
        <td><input name="col1" id="col1"></td> 
        <td><input name="col2" id="col2"></td> 
        <td> 
         <select name="col3" id="col3"> 
         <option value="">Please select</option> 
         <option value="1">select1</option> 
         <option value="2">select1</option> 
         <option value="3">select1</option> 
         </select> 
        </td> 
        </tr> 
       </tbody> 
      </table> 
<a href="#" id="addLink">+</a> 

鏈接被點擊的時候,我想添加一個新行但沒有第三列到表的末尾。所以我用jQuery來做到這一點:

$(document).ready(function(){ 
       $('#addLink').click(function(){ 
        addTableRow($("table")); 
        return false; 
       }); // end click 
      function addTableRow(table) 
      { 
       // get the first row in the table 
       var $tr = $(table).find("tbody tr:first").html(); 

       $($tr).remove('td:last'); // remove the last column 


       $('tbody').append("<tr>"); 
       $(table).find('tbody tr:last').append($tr); 


         }; 
      }); // end ready 

然而,這部分是不刪除第3列:

$($tr).remove('td:last'); // remove the last column 

是有什麼辦法可以追加一個新行只是第2列?

感謝提前:)

+0

刪除( 'TD:EQ(3)') – max4ever

+0

@ max4ever這個人是不工作... –

+0

你想是這樣的http://jsfiddle.net/j08691/ dktqs /? – j08691

回答

1

這是你在找什麼?

我改變了一些其他的東西,你會明白的。

$(document).ready(function(){ 
    $('#addLink').click(function(){ 
    addTableRow($("table")); 
    return false; 
    }); // end click 
    function addTableRow(table) { 
    // get the first row in the table 
    var $tr = table.find("tbody tr:first").html(); 


    $('tbody').append($('<tr/>')); 
    table.find('tbody tr:last').append($tr).find('td:last').remove(); 


    }; 
}); // end ready​ 

http://jsfiddle.net/QTuzM/

+0

是的,這是我想要的。謝謝 –

-1
$('tbody').append("<tr>"); 
       $(table).find('tbody tr:last').append($tr); 
??? 

怎麼樣

$(table).find('tbody').append($tr) 
+0

但重點是remove()函數不會刪除第3列 –

1

沒有必要使用html方法:

$(table).find("tbody tr:first td:last").remove(); 
+0

thx。我在最後一行後添加了這一行:「$(table).find('tbody tr:last td:last')。remove();」它的作品:) –