简体   繁体   中英

Append rows between rows in a table

I have a table in the following structure, there are rows that i would like to append to the table dynamically.

<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>

and i have this jquery that does the appending

$('.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;

});

I want to append rows between the first tr and the last two trs in the tbody. Suggestions!

Try this:

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

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

Here is one way -

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM