-1

我写这段代码是为了删除评论。但是当我删除评论时,它会从数据库中删除,但不会在页面(视图)中删除,所以我需要刷新它,在这种情况下从页面中删除。我怎么解决这个问题 ?

function DeleteNews(id) {
    jQuery.ajax({
        url: "/admin/news/deletenews/" + id,
        type: 'POST',
        dataType: "json",
        success: function (data) {
            if (data === true) {
                alert("خبر با موفقیت حذف گردید");
            } else {
                alert("حذف نشد  . خطایی رخ داده");
            }
        }
        });
    }

看法

<table id="example" class="display" width="100%" cellspacing="0">
    <thead>
        <tr>
            <th>کد خبر</th>
            <th>عنوان خبر</th>
            <th>عملیات</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.ListNews)
        {
            <tr>
                <td id="news(@item.NewsID)">@item.NewsID</td>
                <td>@item.NewsTitle</td>
                <td>
                    <a href="@Url.Action("/EditNews/",new { NewsID=item.NewsID})" class="btn btn-success btn-lg">ویرایش</a>
                    <button type="button" onclick="DetailNews(@item.NewsID)" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
                        جزئیات
                    </button>
                    <button type="button" onclick="NewsComment(@item.NewsID)" class="btn btn-warning btn-lg" data-toggle="modal" data-target="#myModal">
                        نظرات
                    </button>
                    <button type="button" onclick="DetailNews(@item.NewsID)" class="btn btn-default btn-lg" data-toggle="modal" data-target="#myModal">
                        فایل های مریوطه
                    </button>
                    <button class="btn btn-danger btn-lg" onclick="DeleteNews(@item.NewsID)">حذف</button>
                </td>
            </tr>
        }
    </tbody>
</table>
4

2 回答 2

0

你可以试试这样的

function DeleteNews(id) {
        jQuery.ajax({
            url: "/admin/news/deletenews/" + id,
            type: 'POST',
            dataType: "json",
            success: function (data) {
                if (data === true) {
                    alert("data deleted");
                    //below are the different ways to remove the element
                    $('#post-id-'+id).remove(); // removes the element itself leaving others untouched
                    $('#post-id-'+id).empty();// keeps the element but removes all children
                    $('#post-id-'+id).closest("#parent_id").empty(); // travels up the DOM searching for the first parent with the class/id and empties it keeping the parent itself
                    $('#post-id-'+id).closest("#parent_id").remove();// travels up the DOM searching for the first parent and removes it and all its children
                    $('#post-id-'+id).html(' //my new html code here'); // can be used to show that the post has been deleted without showing an alert, much like Facebook does when you unfollow a friend, can also be ("") to empty it
                } else {
                    alert("حذف نشد  . خطایی رخ داده");
                }
            }
        });
    }

修改你的 html 代码有点像这样

<table id="example" class="display" width="100%" cellspacing="0">
    <thead>
        <tr>
            <th>کد خبر</th>
            <th>عنوان خبر</th>
            <th>عملیات</th>

        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.ListNews)
        {
            <tr id="post-id-news(@item.NewsID)"">
                <td id="news(@item.NewsID)">@item.NewsID</td>
                <td>@item.NewsTitle</td>
                <td>
                    <a href="@Url.Action("/EditNews/",new { NewsID=item.NewsID})" class="btn btn-success btn-lg">ویرایش</a>
                    <button type="button" onclick="DetailNews(@item.NewsID)" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
                        جزئیات
                    </button>
                    <button type="button" onclick="NewsComment(@item.NewsID)" class="btn btn-warning btn-lg" data-toggle="modal" data-target="#myModal">
                        نظرات
                    </button>
                    <button type="button" onclick="DetailNews(@item.NewsID)" class="btn btn-default btn-lg" data-toggle="modal" data-target="#myModal">
                        فایل های مریوطه
                    </button>
                    <button class="btn btn-danger btn-lg" onclick="DeleteNews(@item.NewsID)">حذف</button>
                </td>
            </tr>
        }
    </tbody>
</table>
于 2017-02-22T09:26:26.547 回答
0
=== // It's a strong comparison


function DeleteNews(id) {
    jQuery.ajax({
        url: "/admin/news/deletenews/" + id,
        type: 'POST',
        dataType: "json",
        success: function (data) {
            if (data) { // Just it
                // And here is select your comment element and remove him
                $('#comment-' + id).remove();
                alert("Comment removed");
            } else {
                alert("Failed");
            }
        }
    });
}
于 2017-02-22T09:23:27.657 回答