2015-04-02 122 views
0

我有一个添加一组新字段的按钮。在字段本身按钮是删除自己与fieldset但删除不起作用。我的错误在哪里?无法删除()html元素

<fieldset id="adjust_field"><a id="add_new_field"> Add </a></fieldset> 
<script> jQuery(document).ready(function($){ 
     $("a#add_new_field").click(function(){ 
      var inserted_new_field = '<fieldset id="to_be_removed"></fieldset><a id="remove_new_field" > Remove </a>'; 
      $("fieldset#adjust_field").append(inserted_new_field); 
     }); 
    }); 
jQuery(document).ready(function($){ 
     $("a#remove_new_field").click(function(){ 

      $("fieldset#to_be_removed").remove(); 
      $('fieldset#to_be_removed').children().remove(); // Tried and this option 
     }); 
    }); 

+0

请你重现问题的小提琴? – Lal 2015-04-02 15:06:06

回答

4

您需要使用事件代表团作为a#remove_new_field动态添加:

$('fieldset#adjust_field').on('click','a#remove_new_field',function(){ 
     $("fieldset#to_be_removed").remove(); 
}); 
+0

完美的工作!谢谢! – worldwildwebdev 2015-04-02 15:28:35