简体   繁体   English

父母删除不适用于附加元素

[英]parents remove doesn't work on appended elements

I'm trying to remove a div after it the remove link on that div is clicked. 我试图删除一个div后,单击该div上的删除链接。 The divs are generated using append after the website is loaded. 网站加载后,使用append生成div。

Appended HTML: 附加的HTML:

<div class="reviews">
<div class="row-fluid" id="57">
    <div class="span2">
        <img src="img/default_male.png" alt="Male Default Photo">
    </div>
    <div class="span10">
        <h6>Test Test</h6>
        <img class="rating-stars" src="img/2star.png" alt="2 Stars">
        <br>
        <p class="comment-info">
            <time class="timeago" datetime="2013-09-19 22:49:59" title="Thursday, Sep 19, 2013 - 10:49 PM">5 minutes ago</time>, 
            <a href="#removereview" class="removereview">remove</a>
        </p>
    </div>
</div>

My JQuery Code: 我的JQuery代码:

            $(".reviews").on('click', '.removereview', function() {
            var review_id = $(this).parents('.row-fluid').attr('id');
            console.log(review_id);

            $.ajax({
                type: "POST",
                url: "event_send.php",
                data: "func=remove_review&post_id=" + review_id + "&event_name=" + getUrlVars()["event_name"],
                success: function(response){
                    $(".review-count").html(parseInt($(".review-count").html()) - 1);
                    $(this).closest('.row-fluid').remove();
                }
            });

            return false;
        });

The problem is that the div doesn't get removed although the right ID is chosen by the code. 问题是,尽管代码选择了正确的ID,但div不会被删除。 Any ideas on how to fix the problem? 关于如何解决问题的任何想法?

Thank you 谢谢

this inside the success handler does not refers the clicked element so your selector $(this).closest('.row-fluid') fails to find the element. 成功处理程序中的this不会引用被单击的元素,因此您的选择器$(this).closest('.row-fluid')找不到该元素。

One solution is to use a closure variable self which then will be used inside the success handler 一种解决方案是使用闭包变量self ,然后将其用于成功处理程序中

$(".reviews").on('click', '.removereview', function() {
    var review_id = $(this).parents('.row-fluid').attr('id');
    console.log(review_id);

    var self = this;
    $.ajax({
        type: "POST",
        url: "event_send.php",
        data: "func=remove_review&post_id=" + review_id + "&event_name=" + getUrlVars()["event_name"],
        success: function(response){
            $(".review-count").html(parseInt($(".review-count").html()) - 1);
            $(self).closest('.row-fluid').remove();
        }
    });

    return false;
});

Another is to use $.proxy() to pass a custom context to the success callback 另一个方法是使用$ .proxy()将自定义上下文传递给成功回调

$(".reviews").on('click', '.removereview', function() {
    var review_id = $(this).parents('.row-fluid').attr('id');
    console.log(review_id);

    $.ajax({
        type: "POST",
        url: "event_send.php",
        data: "func=remove_review&post_id=" + review_id + "&event_name=" + getUrlVars()["event_name"],
        success: $.proxy(function(response){
            $(".review-count").html(parseInt($(".review-count").html()) - 1);
            $(this).closest('.row-fluid').remove();
        }, this)
    });

    return false;
});

I believe you are trying to remove the element you got in review_id ; 我相信您正在尝试删除您在review_id获得的元素; Why not remove it directly? 为什么不直接将其删除?

$('#'+review_id).remove();

correct me if I misunderstood your question. 如果我误解了你的问题,请纠正我。

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

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