Delete dynamically-generated table row using jQuery

前端 未结 6 2060
情深已故
情深已故 2020-12-31 02:28

the code below add and remove table row with the help of Jquery the add function works fine but the remove only work if I remove the first row

         


        
6条回答
  •  醉酒成梦
    2020-12-31 03:15

    A simple solution is encapsulate code of button event in a function, and call it when you add TRs too:

     var i = 1;
    $("#addbutton").click(function() {
      $("table tr:first").clone().find("input").each(function() {
        $(this).val('').attr({
          'id': function(_, id) {return id + i },
          'name': function(_, name) { return name + i },
          'value': ''               
        });
      }).end().appendTo("table");
      i++;
    
      applyRemoveEvent();  
    });
    
    
    function applyRemoveEvent(){
        $('button.removebutton').on('click',function() {
            alert("aa");
          $(this).closest( 'tr').remove();
          return false;
        });
    };
    
    applyRemoveEvent();
    

    http://jsfiddle.net/Z7fG7/2/

提交回复
热议问题