简体   繁体   English

jQuery删除所有附加元素

[英]jquery removing all appended elements

I'm trying to code a todo list form. 我正在尝试编写待办事项列表表单。 this form can contain unlimited rows. 此表格可以包含无限的行。 the rows each contain a text input field with 2 buttons beside it. 每行包含一个文本输入字段,旁边有2个按钮。 1 of the buttons adds a new row below, the second button deletes the row. 其中一个按钮在下面添加新行,第二个按钮删除该行。

The problem is, when I click the add button a bunch of times, it removes ALL of the elements that were added below it when i click on the delete icon for that row. 问题是,当我多次单击添加按钮时,当我单击该行的删除图标时,它将删除添加在其下方的所有元素。

I hope im making sense, maybe the code will explain whats going on. 我希望即时消息有意义,也许代码可以解释发生了什么。

Heres the PHP code for the todo-list form page: 以下是待办事项列表表单页面的PHP代码:

<div class="table table-todo">

    <?php include 'projects-add-task.php'; ?>

</div>

Here is the contents of projects-add-task.php : 这是projects-add-task.php的内容:

<div class="field">
<input type="text" name="task[]" />
<a href="#" title="Add Task Below" class="todo-add"><img src="images/icon-todo-add.png" alt="" onmouseover="this.src='images/icon-todo-add-h.png'" onmouseout="this.src='images/icon-todo-add.png'" /></a>
<?php if ($_GET['show_delete'] == 'yes') { ?>
<a href="#" title="Delete This Task" class="todo-delete"><img src="images/icon-todo-delete.png" alt="" onmouseover="this.src='images/icon-todo-delete-h.png'" onmouseout="this.src='images/icon-todo-delete.png'" /></a>
<?php } ?>
<div style="clear: both;"></div>
</div>

Here is the jquery that isnt working 100% correctly: 这是无法正常工作100%的jquery:

$('.todo-add').live('click', function (e) { 
    e.preventDefault();
    $parent = $(this).parent('.field');     
    $.get('projects-add-task.php?show_delete=yes', function (data) { $parent.append(data); }, 'html');
});

$('.todo-delete').live('click', function (e) {  
    e.preventDefault();
    $(this).parent('.field').remove();
});

I had to use live for the .todo-add icon because it was not working obviously for the newly appended rows. 我必须对.todo-add图标使用live ,因为它显然不适用于新添加的行。 however im unsure if this is necessary for the .todo-delete icon, but i did it just to be sure. 但是我不确定这是否是.todo-delete图标所必需的,但是我只是为了确保做到这一点。

Any help would be greatly appreciated with this issue. 任何帮助将不胜感激与此问题。

I think you need after and not append : 我认为您需要after而不是append

function (data) { $parent.append(data); }

should be 应该

function (data) { $parent.after(data); }

Also, live is actually deprecated now. 此外, live现在实际上已被弃用。 You should be using on . 你应该使用on

you are adding many divs with the class "field" and then asking jquery to delete them on the delete button. 您将使用“ field”类添加许多div,然后要求jquery在“删除”按钮上将其删除。

you need to either have unique ID for each row and then delete that ID element os simply work witht he parent of the delete button, no need to specify the '.field' in the parent() 您需要为每行具有唯一的ID,然后删除该ID元素,只需使用Delete按钮的父元素,而无需在parent()中指定“ .field”

$('.todo-delete').live('click', function (e) {  
    e.preventDefault();
    $(this).parent().remove();
});

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

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