Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

javascript - Cannot get parent element using jQuery

I have the following html structure created dynamically by foreach loop and tried to remove the whole element by accessing it from (ACTIVE HYPERLINK). I tried many different way, but cannot reach to it. As the whole block (of ACTIVE HYPERLINK) is repeated, I think it is meaningless to use class name of the hyperlink. I also tried to use a.active but not seem to work.

@foreach (var file in Model.FileAttachments)
{
    <li class="aaa">
        <div class="bbb">
            <div class="ccc">
                <div class="ddd">                   
                    <div class="eee">
                        <ul class="fff">
                            <li>
                                <a class="xxx" href="javascript:void(0);" data-id="@file.Id" data-toggle="confirmation" ></a> <!-- ACTIVE HYPERLINK -->
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </li>
}

<script>

    $('.xxx').click(function (e) {
        e.preventDefault();
        $('[data-toggle="confirmation"]').confirmation('show');
    });


    $('[data-toggle="confirmation"]').confirmation({
        //code omitted for brevity      
        onConfirm: function () { deleteRecord(); }, // Set event when click at confirm button
    });


    function deleteRecord() {
        var $ctrl = $('.xxx');

        $.ajax({
        //code omitted for brevity

        success: function (response, textStatus, XMLHttpRequest) {
            if (response.success) {             
                ctrl.closest('.aaa').remove();
                //or                
                $("a.active").closest('.jFiler-item').remove();
            }
        });

    };

</script>

Here is some example of my tries:

$("a.active").closest('.aaa').remove();
$(".xxx").closest('.aaa').remove();
$(this).data('.aaa')remove();
$("a.active").parents('li').eq(2)remove();
$(".xxx").parents('li').eq(2)remove();

Any idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

UPDATED RESPONSE

$('.xxx').click(function (e) {
    var $this = $(this);
    e.preventDefault();
    $('[data-toggle="confirmation"]').confirmation({
        //code omitted for brevity      
        onConfirm: function () {
            deleteRecord($this); // send reference to delete method
        }
    });
    $('[data-toggle="confirmation"]').confirmation('show');
});

...

function deleteRecord($ctrl) {
...

ORIGINAL RESPONSE

If you want the block removed on click on the <a> element, you need to assign a handler to the click event:

$('.xxx').on('click', function(){
    $(this).closest('.aaa').remove();
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...