简体   繁体   English

如何使用jQuery删除父div?

[英]How can I remove a parent div using jquery?

I want to remove the first div and everything else inside. 我想删除第一个div和其中的所有其他内容。 Here in the code snippet I can delete the first input, but I cant delete the others if I add more. 在代码段中,我可以删除第一个输入,但是如果添加更多,则无法删除其他输入。

On my page I can't delete any input. 在我的页面上,我无法删除任何输入。

 $(document).ready(function() { var max_fields = 4; //maximum input boxes allowed //var wrapper = $(".phone-field"); //Fields wrapper var add_button = $("#add_field_button"); //Add button ID var x = 1; //initlal text box count $(add_button).click(function(){ //on add input button click if(x < max_fields){ //max input box allowed $(add_button). x++; //text box increment $('.phone-field').append('<div class="col-xs-12 col-md-7 phone-class"><label for="telefones">Telefone</label><div class="input-group"><input type="text" class="form-control" name="telefone[]" placeholder="Digite seu telefone"><span class="input-group-btn"><button class="btn btn-default" id="remove_field" type="button"><i class="fa fa-trash-o" aria-hidden="true"></i></button></span></div></div>'); //add input box } }); $("#remove_field").click(function(){ //user click on remove text //e.preventDefault(); //$('#remove_field').closest('div.phone-class').remove(); $('#remove_field').parent().parent().parent().remove(); }); }); 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://use.fontawesome.com/1cdb0cfd25.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="form-group phone-field"> <div class="col-xs-12 col-md-7"> <label for="telefones">Telefone</label> <div class="input-group"> <input type="text" class="form-control" name="telefone[]" placeholder="Digite seu telefone"> <span class="input-group-btn"> <button class="btn btn-default" id="add_field_button" type="button"><i class="fa fa-plus" aria-hidden="true"></i></button> </span> </div> </div> <div class="col-xs-12 col-md-7 phone-class"> <!-- I want to delete this DIV here if i click on the button with id="remove_field"--> <label for="telefones">Telefone</label> <div class="input-group"> <input type="text" class="form-control" name="telefone[]" placeholder="Digite seu telefone"> <span class="input-group-btn"><button class="btn btn-default" id="remove_field" type="button"><i class="fa fa-trash-o" aria-hidden="true"></i></button></span> </div> </div> </div> 

That happens because the new elements with id remove_field didn't existed by the time your ready function ran. 发生这种情况是因为在您的就绪函数运行时不存在ID为remove_field的新元素。 This way the only elements bonded to the function that removes the field is the one that already is on the DOM element. 这样,绑定到删除字段的函数的唯一元素就是DOM元素上已经存在的元素。

Luckily for you that's a pretty common mistake and a fair simple one to solve also. 幸运的是,这是一个很常见的错误,而且也很容易解决。 You just need to bind an permanent parent element using jQuery's .on function: 您只需要使用jQuery的.on函数绑定一个永久的父元素:

 $(document).ready(function() { var max_fields = 4; //maximum input boxes allowed //var wrapper = $(".phone-field"); //Fields wrapper var add_button = $("#add_field_button"); //Add button ID var x = 1; //initlal text box count $(add_button).click(function(){ //on add input button click if(x < max_fields){ //max input box allowed $(add_button). x++; //text box increment $('.phone-field').append('<div class="col-xs-12 col-md-7 phone-class"><label for="telefones">Telefone</label><div class="input-group"><input type="text" class="form-control" name="telefone[]" placeholder="Digite seu telefone"><span class="input-group-btn"><button class="btn btn-default" id="remove_field" type="button"><i class="fa fa-trash-o" aria-hidden="true"></i></button></span></div></div>'); //add input box } }); $(".phone-field").on('click','#remove_field',function(){ //user click on remove text //e.preventDefault(); //$('#remove_field').closest('div.phone-class').remove(); $('#remove_field').parent().parent().parent().remove(); }); }); 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://use.fontawesome.com/1cdb0cfd25.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="form-group phone-field"> <div class="col-xs-12 col-md-7"> <label for="telefones">Telefone</label> <div class="input-group"> <input type="text" class="form-control" name="telefone[]" placeholder="Digite seu telefone"> <span class="input-group-btn"> <button class="btn btn-default" id="add_field_button" type="button"><i class="fa fa-plus" aria-hidden="true"></i></button> </span> </div> </div> <div class="col-xs-12 col-md-7 phone-class"> <!-- I want to delete this DIV here if i click on the button with id="remove_field"--> <label for="telefones">Telefone</label> <div class="input-group"> <input type="text" class="form-control" name="telefone[]" placeholder="Digite seu telefone"> <span class="input-group-btn"><button class="btn btn-default" id="remove_field" type="button"><i class="fa fa-trash-o" aria-hidden="true"></i></button></span> </div> </div> </div> 

1) When you are inside a listener use "this" to refer to the object 1)在侦听器内部时,请使用“ this”来引用对象

2) Use closest instead of parent().parent().parent() or your function will be useless if HTML changes: 2)使用最接近的值而不是parent()。parent()。parent(),否则如果HTML更改,则您的函数将无用:

3) Make "remove_field" a class, as you have multiple elements with the same id, and as previous comment stated that is not valid HTML 3)将“ remove_field”设为一个类,因为您有多个具有相同ID的元素,并且如先前的注释所述,这是无效的HTML

$(this).closest('div.phone-class').remove(); $(this).closest('div.phone-class')。remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM