jQuery Clone table row

前端 未结 7 1124
悲&欢浪女
悲&欢浪女 2020-11-30 03:56

I have a table with an Add button on the end. When you click this button I want a new table row to be created underneath the current one. I also want the input fields on thi

相关标签:
7条回答
  • 2020-11-30 04:07

    Try this variation:

    $(".tr_clone_add").live('click', CloneRow);
    
    function CloneRow()
    {
        $(this).closest('.tr_clone').clone().insertAfter(".tr_clone:last");
    }
    
    0 讨论(0)
  • 2020-11-30 04:09

    Is very simple to clone the last row with jquery pressing a button:

    Your Table HTML:

    <table id="tableExample">
        <thead>
            <tr>
                <th>ID</th>
                <th>Header 1</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1</td>
                <td>Line 1</td>
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="2"><button type="button" id="addRowButton">Add row</button></td>
            </tr>
        </tfoot>
    </table>
    

    JS:

    $(document).on('click', '#addRowButton', function() {
        var table = $('#tableExample'),
            lastRow = table.find('tbody tr:last'),
            rowClone = lastRow.clone();
    
        table.find('tbody').append(rowClone);
    });
    

    Regards!

    0 讨论(0)
  • Your problem is that your insertAfter:

    .insertAfter(".tr_clone")
    

    inserts after every .tr_clone:

    the matched set of elements will be inserted after the element(s) specified by this parameter.

    You probably just want to use after on the row you're duplicating. And a little .find(':text').val('') will clear the cloned text inputs; something like this:

    var $tr    = $(this).closest('.tr_clone');
    var $clone = $tr.clone();
    $clone.find(':text').val('');
    $tr.after($clone);
    

    Demo: http://jsfiddle.net/ambiguous/LAECx/ or for a modern jQuery: http://jsfiddle.net/ambiguous/LAECx/3274/

    I'm not sure which input should end up with the focus so I've left that alone.

    0 讨论(0)
  • 2020-11-30 04:24

    Here you go:

    $( table ).delegate( '.tr_clone_add', 'click', function () {
        var thisRow = $( this ).closest( 'tr' )[0];
        $( thisRow ).clone().insertAfter( thisRow ).find( 'input:text' ).val( '' );
    });
    

    Live demo: http://jsfiddle.net/RhjxK/4/


    Update: The new way of delegating events in jQuery is

    $(table).on('click', '.tr_clone_add', function () { … });
    
    0 讨论(0)
  • 2020-11-30 04:24

    Try this code, I used the following code for cloning and removing the cloned element, i have also used new class (newClass) which can be added automatically with the newly cloned html

    for cloning..

     $(".tr_clone_add").live('click', function() {
        var $tr    = $(this).closest('.tr_clone');
        var newClass='newClass';
        var $clone = $tr.clone().addClass(newClass);
        $clone.find(':text').val('');
        $tr.after($clone);
    });
    

    for removing the clone element.

    $(".tr_clone_remove").live('click', function() { //Once remove button is clicked
        $(".newClass:last").remove(); //Remove field html
        x--; //Decrement field counter
    });
    

    html is as followinng

    <tr class="tr_clone">
                           <!-- <td>1</td>-->
                            <td><input type="text" class="span12"></td>
                            <td><input type="text" class="span12"></td>
                            <td><input type="text" class="span12"></td>
                            <td><input type="text" class="span12"></td>
                            <td><input type="text" class="span10" readonly>
                            <span><a href="javascript:void(0);" class="tr_clone_add" title="Add field"><span><i class="icon-plus-sign"></i></span></a> <a href="javascript:void(0);" class="tr_clone_remove" title="Remove field"><span style="color: #D63939;"><i class="icon-remove-sign"></i></span></a> </span> </td> </tr>
    
    0 讨论(0)
  • 2020-11-30 04:27

    Try this.

    HTML

    <!-- Your table -->
    <table width="100%" border="0" cellspacing="0" cellpadding="0" id="table-data"> 
        <thead>
            <tr>
                <th>Name</th>
                <th>Location</th>
                <th>From</th>
                <th>To</th>
                <th>Add</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="text" autofocus placeholder="who" name="who" ></td>
                <td><input type="text" autofocus placeholder="location" name="location" ></td>
                <td><input type="text" placeholder="Start Date" name="datepicker_start" class="datepicker"></td>
                <td><input type="text" placeholder="End Date" name="datepicker_end" class="datepicker"></td>
                <td><input type="button" name="add" value="Add" class="tr_clone_add"></td>
            </tr>
        <tbody>
    </table>
    
    <!-- Model of new row -->
    <table id="new-row-model" style="display: none"> 
        <tbody>
            <tr>
                <td><input type="text" autofocus placeholder="who" name="who" ></td>
                <td><input type="text" autofocus placeholder="location" name="location" ></td>
                <td><input type="text" placeholder="Start Date" name="datepicker_start" class="datepicker"></td>
                <td><input type="text" placeholder="End Date" name="datepicker_end" class="datepicker"></td>
                <td><input type="button" name="add" value="Add" class="tr_clone_add"></td>
            </tr>
        <tbody>
    </table>
    

    Script

    $("input.tr_clone_add").live('click', function(){
        var new_row = $("#new-row-model tbody").clone();
        $("#table-data tbody").append(new_row.html());
    });
    
    0 讨论(0)
提交回复
热议问题