简体   繁体   English

jQuery:如何删除表中的 tr 标签?

[英]jQuery: How to remove a tr tag in a table?

I want to delete a tr tag after clicking delete button using jQuery but it is not working, I used remove method of jQuery.我想在使用 jQuery 单击删除按钮后删除 tr 标签,但它不起作用,我使用了 jQuery 的 remove 方法。 This is HTML code这是 HTML 代码

<tbody>
<tr class="image-col"  th:each="image : ${session.galleryList}">
    <td><div class="col-sm-4 proj_gallery_grid section_1_gallery_grid">
                <div class="section_1_gallery_grid1">
                    <img th:src="@{'/images/' + ${image.imageURL}}" alt=" " class="img-fluid" />
                </div>
        </div>
    </td>
    <td th:text="${image.title}"></td>
    <td>
    <i style="display: none;" class="image-id" th:text="${image.id}"></i>
    <button class="delete-image-btn btn btn-success btn-block">Delete Image</button></td>
</tr>

And this is jQuery code.这是 jQuery 代码。

$(document).on('click', '.delete-image-btn', function(event) {
    event.preventDefault();
    if (confirm("Are youe sure to delete this image?")) {

        var message = "Image deleted successfully";
        var imageId = $(this).parent().find(".image-id").text();
        $.ajax({
            type: "GET",
            url: "/image/delete_image?imid=" + imageId,
            success: function(result) {
                if (result.status) {
                    $(this).parent().find('.image-col').remove();
                } else {
                    alert(result.message);
                }
            },
            error: function() {
                alert("Error! Please try again");
            }
        });
        return;
    } else {
        return;
    }
});

you can do this你可以这样做

$('.delete-image-btn').click(function(){
  $(this).parents('tr').remove();
})

Your DOM search for tr with class is wrong.您使用class搜索tr DOM 是错误的。 Parent of your button is td not tr .您的按钮的父级是td而不是tr So to remove tr you can use closest to find tr and remove.因此,要删除tr您可以使用closest来查找tr并删除。

$(this).closest("tr").remove();

Like:喜欢:

$(document).on('click', '.delete-image-btn', function(event) {
 var $this = $(this); // Hold your this state before ajax call

After that :在那之后 :

$this.closest("tr").remove();

Use _t.parent().parent().remove();使用_t.parent().parent().remove();

 $(document).on('click', '.delete-image-btn', function(){ var _t = $(this); // event.preventDefault(); if(confirm("Are youe sure to delete this image?")){ _t.parent().parent().remove(); var message = "Image deleted successfully"; var imageId = $(this).parent().find(".image-id").text(); return false; } else { return false; } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <tbody> <tr class="image-col" th:each="image : ${session.galleryList}"> <td><div class="col-sm-4 proj_gallery_grid section_1_gallery_grid"> <div class="section_1_gallery_grid1"> <img th:src="@{'/images/' + ${image.imageURL}}" alt=" " class="img-fluid" /> </div> </div> </td> <td th:text="${image.title}"></td> <td> <i style="display: none;" class="image-id" th:text="${image.id}"></i> <button class="delete-image-btn btn btn-success ">Delete Image</button> </td> </tr>

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

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