簡體   English   中英

在表中的行之間追加行

[英]Append rows between rows in a table

我有一個以下結構的表,我想動態地將一些行附加到表中。

<table>
    <thead>
        <tr>
            <td>Work</td>
            <td>Award</td>

        </tr>
    </thead>
    <tbody>
        <tr>
           <td><input class="w_name" type='text'/></td>
           <td><input class="other" type='text'/></td>

        </tr>
            <tr>
             <td><input class="w_name" readonly/></td>
            <td><input class="other" type='text'/></td>

        </tr>
            <tr>
           <td><input class="w_name" type='text'/></td>
            <td><input class="other" type='text'/></td>

        </tr>
    </tbody>
</table>

我有這個 jquery 做附加

$('.btn-add').click(function() {
    var index = $('tr', $(this).closest("tbody")).index(this);
    alert(index);
    var tbody = $('tbody');
    var i = $('tbody tr').size() + 1;            
    tablerow="<tr><td><input class=\"w_name\" type='text'/></td> <td><input class=\"other\" type='text'/></td><td><input class=\"other\" type='text'/></td> <td><input class=\"other\"type='text'/></td><td><input class=\"other\" type='text'/></td><td><input class=\"other\" type='text'/><a href=\"#\" id=\"remScnt\">Remove</a></td><tr>";
     tbody.append(tablerow);
     i++;
     return false;

});

我想在 tbody 的第一個 tr 和最后兩個 trs 之間追加行。 建議!

嘗試這個:

$('#tableidhere > tbody > tr:first-child').after(tablerow)

http://jsfiddle.net/H6jH9/3/

這是一種方法 -

var newRow = "<tr><td><input class=\"w_name\" type='text'/></td> <td><input class=\"other\" type='text'/></td><td><input class=\"other\" type='text'/></td> <td><input class=\"other\"type='text'/></td><td><input class=\"other\" type='text'/></td><td><input class=\"other\" type='text'/><a href=\"#\" id=\"remScnt\">Remove</a></td><tr>";
$('.add').click(function() {
    $(newRow).insertAfter($('tbody tr:first-child'));
});

http://jsfiddle.net/yA4CV/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM