简体   繁体   English

jQuery无法删除父项?

[英]jQuery cannot delete parent?

I have an element inside a div that is "draggable" jquery ui. 我在“可拖动的” jQuery UI的div中有一个元素。 And I am trying to remove that entire div. 我正在尝试删除整个div。 My element looks like 我的元素看起来像

<div class="draggable">
    <img ...>
    <div class="delete">CLICK TO DELETE</div>
</div>

And it has a draggable event on it. 它上面有一个可拖动事件。 And I want it so when you click on the delete button it deletes that entire div. 我想要它,所以当您单击删除按钮时,它会删除整个div。 But it does not seem to work. 但这似乎不起作用。 I've tried $(this).parent/parents.remove and neither of those work, they don't even return any kind of an object ( console.log($(this).parent()); returns []). 我试过$(this).parent/parents.remove ,但它们都不起作用,它们甚至不返回任何对象( console.log($(this).parent());返回[]) 。

My delete function looks like 我的删除功能看起来像

// Delete Image functionality
    $('.delete').click(function() {

        $this.parent().draggable('disable');

        // Post the DELETE request
        $.get('delete/', { d: '1' , id:photo }, function(data) {
            if (data == "true") {
                $(this).parent().remove();
            } else {
                alert("An error has been encountered, please try again later");
            }
        });
    });

Any idea what's up? 知道发生了什么吗?

The problem is that, within the callback to $.get , this is set to the jqXHR instance. 问题在于,在$.get的回调中, this被设置为jqXHR实例。 So you can do, for instance, this.responseText , should you wish. 因此,您可以根据需要执行this.responseText Obviously it is no longer set to the element. 显然,它不再设置为元素。

You need to give the element an alias that isn't overwritten: 您需要为元素提供一个不会被覆盖的别名:

$('.delete').click(function() {
    var clicked = this;
    $this.parent().draggable('disable');

    // Post the DELETE request
    $.get('delete/', {d:'1',id:photo}, function(data) {
        if(data == "true") {
            $(clicked).parent().remove();
        }else{
            alert("An error has been encountered, please try again later");
        }
    });
});

There are various other ways you could do it: the most efficient would be var parent = $(this).parent().draggable('disable') (presuming draggable is chainable, which it probably is). 还有其他多种方法可以执行此操作:最有效的方法是var parent = $(this).parent().draggable('disable') (假定draggable是可链接的,可能是可链接的)。 Another option would be to go by the event's currentTarget property: 另一个选择是通过事件的currentTarget属性:

$('.delete').click(function(event) {
    // ...
            $(e.currentTarget).parent().remove();

I think the version I've given is probably the clearest at showing why this didn't work for you, which is why I chose it. 我认为我提供的版本可能最清楚地表明了为什么它对您不起作用,这就是我选择它的原因。

"this" inside of the inner function is different from "this" in the outer scope. 内部函数内部的“ this”与外部范围内的“ this”不同。 Try storing "this" in another variable, and using that variable within the outer function, ie: 尝试将“ this”存储在另一个变量中,并在外部函数中使用该变量,即:

// Delete Image functionality
    $('.delete').click(function() {

        $this.parent().draggable('disable');
        var that = $(this);

        // Post the DELETE request
        $.get('delete/', {d:'1',id:photo}, function(data) {
            if(data == "true") {
                that.parent().remove();
            }else{
                alert("An error has been encountered, please try again later");
            }
        });
    });

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

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