2013-03-08 99 views
0

我正在使用下面的代码,通过单击按钮添加表格行,现在我需要另一个按钮来删除新创建的行。请帮助我做到这一点。谢谢删除添加的html表格行

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js">  </script> 

<script> 
jQuery(function(){ 
var counter = 1; 
jQuery('input.add_row').click(function(event){ 
    event.preventDefault(); 
    counter++; 
    var newRow = jQuery('<tr><td><select name="level' + counter + '"><option value="Matric or equivalent">Matric or equivalent</option><option value="10+2 or equivalent">10+2 or equivalent</option><option value="Diploma after matric">Diploma after matric</option><option value="Bachelor Degree">Bachelor Degree</option><option value="Master Degree">Master Degree</option><option value="Others">Others</option></select></td><td><input type="text" name="prog' + 
     counter + '"/></td><td><input type="text" name="ins' + 
     counter + '"/></td><td><input type="text" name="univ' + 
     counter + '"/></td><td><input type="text" name="reg' + 
     counter + '"/></td><td><input type="text" name="yr' + 
     counter + '" size="4" maxlength="4"/></td><td><input type="text" name="mar' + 
     counter + '"/></td><td><input type="text" name="sub' + 
     counter + '" size="8"/></td></tr>'); 
    jQuery('table.aca_inf').append(newRow); 
}); 
}); 
</script> 

回答

2

尝试:

jQuery('input.del_row').click(function(event){ 
    event.preventDefault(); 
    jQuery('table.aca_inf tr:last').remove(); 

}); 
+0

A.V谢谢。作品不错.. !! – MEP 2013-03-08 09:21:07

+0

欢迎..也可以通过'counter - '递减计数器,但是当删除一个并添加另一个时会创建重复项... – Anujith 2013-03-08 09:24:24

+0

帮助我...使用javascript新创建的行的验证不起作用: ( – MEP 2013-03-08 12:08:13

1

使用:

function delete_row(){ 
    //delete last row of table 
    while(counter){ 
    $('#btn').on("click", function(){ 
      $('#tab1 tr:last').remove(); 
      counter--; 
    }); 
    } 
    } 
+0

感谢您的努力..! – MEP 2013-03-08 09:24:38