jQuery .parent().remove() 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6010260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:13:16  来源:igfitidea点击:

.parent().remove() issue

jqueryformsclone

提问by leapin_leprechaun

I have a jQuery-based form where you can add extra people to the application. I'm cloning the first fieldset and adding it onto the end up to a max of 3 additional people. When you've added 1 extra person then you have the option to remove that person.

我有一个基于 jQuery 的表单,您可以在其中向应用程序添加额外的人员。我正在克隆第一个字段集并将其添加到最后最多 3 个人。当您添加了 1 个额外的人时,您可以选择删除该人。

However, my remove button isn't working. It was, earlier, until I added the extra functions to the cloning to change the ids of other elements within the fieldset.

但是,我的删除按钮不起作用。早些时候,直到我将额外的功能添加到克隆中以更改字段集中其他元素的 ID。

I'm using:

我正在使用:

$(".remove").click(function() {
    $(this).parent().remove();

which was working originally but now it's not and I can't figure out why.

最初是有效的,但现在不是,我不知道为什么。

I've taken out the lines that stop the first 'delete this person' just to show that the first one still works but the rest don't. (I'll be positioning the first one off stage eventually when it's fixed)

我已经去掉了停止第一个“删除此人”的行,只是为了表明第一个仍然有效,但其余的则无效。(当它修复时,我最终会将第一个放置在舞台外)

Probably easier to see it so I put it up here.

可能更容易看到它,所以我把它放在这里

So essentially, any ideas why my 'delete this person' isn't working on everything but the first section?

所以基本上,有什么想法为什么我的“删除这个人”除了第一部分之外不能处理所有事情?

回答by PeeHaa

Try:

尝试:

$(document).on('click', '.remove', function() {
    $(this).parent().remove();
});

Events are bound on page load so newly added element aren't.

事件在页面加载时绑定,因此新添加的元素不是。

回答by Dave Hilditch

The 'live' function call has been deprecated and no longer works. You can find instructions for how to rewrite functions using the replacement 'on()' method here and more info about the deprecation:

'live' 函数调用已被弃用,不再有效。您可以在此处找到有关如何使用替换“on()”方法重写函数的说明以及有关弃用的更多信息:

http://api.jquery.com/live/

http://api.jquery.com/live/

To handle all current and newly created elements, you must now use:

要处理所有当前和新创建的元素,您现在必须使用:

$(document).on("click", ".remove", function() {
    $(this).parent().remove();
});

jQuery runs on the document object, not the element and there is an additional parameter to specify which elements to watch and add event listeners to.

jQuery 在文档对象上运行,而不是在元素上运行,并且有一个额外的参数来指定要监视的元素并向其添加事件侦听器。

回答by orolo

Make sure you close your expresion:

确保关闭表达式:

$(".remove").click(function() {
    $(this).parent().remove();
});

回答by Andrea_dev

You can do this for delete the parent:

您可以执行此操作来删除父项:

 $(document).on("click", ".remove", function() { 
      $(this).parent().remove(); 
 });

or you can do this for delete all parents:

或者您可以这样做以删除所有父母:

 $(document).on("click", ".remove", function() { 
      $(this).parents().remove();
 });

回答by davin

The elements don't originally exist, which means you need to use .live()or .delegate()

这些元素最初并不存在,这意味着您需要使用.live().delegate()

For example, change:

例如,更改:

use $(".remove").click(...)instead of $(".remove").live("click", ...)this

用这个$(".remove").click(...)代替$(".remove").live("click", ...)