简体   繁体   English

单击以删除父div

[英]Click to remove parent div

I'm struggling for something that seems pretty basic, though I'm not seeing what I am doing wrong. 我正在努力寻找看似非常基本的东西,尽管我没有看到我做错了什么。

I want that onClick a certain div is cloned till the maximum of 4 times. 我想要点击某个div克隆到最多4次。 (So far so good), and I want to have a remove button that deletes one of the divs inserted. (到目前为止一直很好),我想要一个删除按钮,删除插入的一个div。

And my problem is right there. 我的问题就在那里。 I can't get the remove button to work. 我无法让删除按钮工作。

JS JS

$(document).ready(function() {
  var max_fields      = 4; // maximum input boxes allowed
  var wrapper         = $("#addDriverContent div:first"); 
  var add_button      = $(".add_field_button"); // Add button ID
  var x = 0

  $(add_button).click(function(e) {
    e.preventDefault();
    // max input box allowed
    if(x < max_fields) {
      x++; 
      $(wrapper).clone().append('<a href="#" class="remove_field">Remove</a>').appendTo($('#clone_wrapper'));}
  });

  // user click on remove text
  $(wrapper).on("click",".remove_field", function(e) {
    e.preventDefault();
    $(this).parent().sibling('#content').remove();
    x--;
  })

});

HTML HTML

<div id="addDriverContent" style="display:none;">
  <div id="content">
    Contents
  </div>
</div>

<button type="button" class="add_field_button" id="clone_button">ADD DRIVER</button>
<div id="clone_wrapper"></div>

Take a look at my fiddle . 看看我的小提琴

(I've started with this example ) (我从这个例子开始)

There are two problems with your javascript 你的javascript有两个问题

  1. You are attaching the click event handler to wrong element. 您正在将click event handler附加到错误的元素。 The element you are attaching to is not even visible on page and is never clicked. 您附加的元素在页面上甚至不可见,并且从不单击。
  2. Your line where you try to remove the clicked content is wrong. 您尝试删除所点击内容的行是错误的。 $(this).parent().remove() is enough. $(this).parent().remove()就足够了。

See Updated Fiddle 查看更新的小提琴

$(document).ready(function() {
    var max_fields = 4; //maximum input boxes allowed
    var wrapper = $("#addDriverContent div:first");
    var add_button = $(".add_field_button"); //Add button ID
    var x = 0

    $(add_button).click(function(e) {
        e.preventDefault();
        if (x < max_fields) { //max input box allowed
            x++;
            $(wrapper).clone().append('<a href="#" class="remove_field">Remove</a>').appendTo($('#clone_wrapper'));
        }
    });

    $(document).on("click", ".remove_field", function(e) { //user click on remove text
        e.preventDefault();
        $(this).parent().remove();
        x--;
    })
});

Change your event listener to the following: 将您的事件监听器更改为以下内容:

$("#clone_wrapper").on("click",".remove_field", function(e) {
    e.preventDefault(); $(this).parent().remove(); x--;
});

Working Example . 工作实例

I've made two changes: 我做了两处改动:

  1. Change $(wrapper) to $("#clone_wrapper") . $(wrapper)更改$(wrapper) $("#clone_wrapper") The .remove_field links are added to the wrapper clone, not the wrapper itself (from appendTo($('#clone_wrapper')) ) .remove_field链接被添加到包装器克隆,而不是包装器本身(来自appendTo($('#clone_wrapper'))
  2. Change $(this).parent().sibling('#content') to $(this).parent() . $(this).parent().sibling('#content')更改$(this).parent() The parent is the #content — you don't want to remove its sibling. 父级 #content - 你不想删除它的兄弟。

You need to add the new onclick function during the first one, and change your selector like this: 您需要在第一个函数中添加新的onclick函数,并更改您的选择器,如下所示:

$(document).ready(function() {
var max_fields      = 4; //maximum input boxes allowed
var wrapper         = $("#addDriverContent div:first"); 
var add_button      = $(".add_field_button"); //Add button ID
var x = 0

$(add_button).click(function(e) {
e.preventDefault();
 if(x < max_fields){ //max input box allowed
            x++; 
        $(wrapper).clone().append('<a href="#" class="remove_field">Remove</a>').appendTo($('#clone_wrapper'));
        $(".remove_field").click( function(e){ //user click on remove text
            e.preventDefault(); 
            $(this).parent().remove(); 
            x--;
        })
    }
});

});

I refactored your code, which is more clean and effective. 我重构了你的代码,它更干净,更有效。 explanations is in comment. 解释正在评论中。

HTML HTML

<div id="addDriverContent" style="display:none;">

    <!-- if your element is going to cloned, use class instead of id. id should always be unique. -->
    <div class="content">

        <!-- because your content is already invisible, why note put your remove btn in it? -->
        <a href="#" class="remove_field">Remove</a>

    </div>
</div>

<button type="button" class="add_field_button" id="clone_button">ADD DRIVER</button>

<div id="clone_wrapper"></div>

JS JS

$(function($) {

var max_fields        = 4; 

// origin selector would select all the first div ancestors.
var $content          = $("#addDriverContent > .content");     

var $clone_wrapper    = $("#clone_wrapper") ;

var $add_button       = $(".add_field_button"); //Add button ID

$add_button.click(function(e) {
    e.preventDefault();

    // jquery object is array liked object. Length mean how many elements is selected.
    if ( $clone_wrapper.children(".content").length < max_fields ) 

      $content.clone().appendTo($clone_wrapper);
});

$clone_wrapper.on("click",".remove_field", function(e){ 
    e.preventDefault(); 

    // this would be more specific.
    $(this).parent(".content").remove(); 
})

});

https://jsfiddle.net/xm4sesa2/10/ https://jsfiddle.net/xm4sesa2/10/

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

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