Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 删除元素时不更新列表_Jquery_Asp.net_Ajax_Asp.net Mvc_Jquery Ajaxq - Fatal编程技术网

Jquery 删除元素时不更新列表

Jquery 删除元素时不更新列表,jquery,asp.net,ajax,asp.net-mvc,jquery-ajaxq,Jquery,Asp.net,Ajax,Asp.net Mvc,Jquery Ajaxq,我写这段代码是为了删除注释。但当我删除注释时,它会从数据库中删除,但不会在页面(视图)中删除,所以我需要刷新它,在本例中是从页面中删除。我怎样才能解决这个问题 function DeleteNews(id) { jQuery.ajax({ url: "/admin/news/deletenews/" + id, type: 'POST', dataType: "json", success: function (data)

我写这段代码是为了删除注释。但当我删除注释时,它会从数据库中删除,但不会在页面(视图)中删除,所以我需要刷新它,在本例中是从页面中删除。我怎样才能解决这个问题

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


کد خبر
عنوان خبر
عملیات
@foreach(Model.ListNews中的var项)
{
@item.NewsID
@item.NewsTitle
جزئیات
نظرات
فایل های مریوطه
حذف
}

您可以试试这样的方法

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>

کد خبر
عنوان خبر
عملیات
@foreach(Model.ListNews中的var项)
{

您需要在成功回调中从DOM中删除相关元素。您需要在视图中显示代码。删除后,只需发送新数据以绑定为AjaxResult或重定向到新闻列表页。@Amit如何做到这一点?请编写该命令code@StephenMuecke我编辑question@KianoushAmits的建议是从本质上刷新整个视图ch不是您想要的。@Kianoush-这只是一个逻辑。我相信您需要在其中包含正确的元素Id或父Id。@Kianoush单击“删除”按钮时是否有任何错误?然后检查Id中的
,两者的数字应该相同
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("حذف نشد  . خطایی رخ داده");
                }
            }
        });
    }
<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>