jQuery Clone table row

前端 未结 7 1159
悲&欢浪女
悲&欢浪女 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: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

    
                           
                            
                            
                            
                            
                            
                                
    

提交回复
热议问题