今天遇到了一个问题,Jquery异步删除无效,我是这么写的(看下图)
$(this).parent().parent().remove()直接这么写,数据库删除是没有问题的,但是网页却无法动态删除,只能刷新页面才能显示最新的结果,这显然不是我想要的结果。
实际上只需要这么改(先中间折腾下),定义一个变量var tr来接收一下,之后再通过tr.remove()就好了。
var tr = $(this).parent().parent();
tr.remove();
上码:
@{
ViewBag.Title = "新闻管理";
Layout = "~/Areas/WebHotelManage/Views/Shared/_LayoutManage.cshtml";
}
@using Models
<!-- 自定义 -->
<link rel="stylesheet" type="text/css" href="~/css/AdminCss/NewsManager.css" />
<div class="container content_text">
<div class="table-responsive">
<table class="table table_color table-striped">
<tr class="book_top">
<th>发布时间</th>
<th>新闻标题</th>
<th>新闻分类</th>
<th>操作</th>
</tr>
@{
foreach (News item in ViewBag.listNew)
{
<!-- 新闻1 -->
<tr>
<td>@WebHotel.Models.UtilityHelper.ToShortDate(item.PublishTime)</td>
<td>
<a href='/News/NewsDetail?newsId=@item.NewsId' target="_blank">
@item.NewsTitle
</a>
</td>
<td>@item.NewsCategory.CategoryName</td>
<td class="book-title">
<a class="" href="/WebHotelManage/News/ShowNewsDetail?newsId=@item.NewsId&categoryId=@item.CategoryId">修改</a>
@*<a href="/WebHotelManage/News/DeleteNews?newsId=@item.NewsId" onclick="return confirm('确定要删除吗?')">删除</a>*@
<a name=@item.NewsId class="btndelete" style="cursor:pointer">删除</a>
</td>
</tr>
}
}
</table>
</div>
</div>
@section footer{
<script type="text/javascript">
$(function () {
$(".btndelete").click(function () {
if (!confirm("确定要删除吗?")) { return; }
var newid = $(this).attr("name");
var tr = $(this).parent().parent();
$.post("/WebHotelManage/News/DeleteNews", { "newsId": newid }, function (data, status) {
if (data == "success") {
tr.remove();
}
else {
alert("删除失败");
}
})
})
})
</script>
}
不过有个问题,为什么会这样,还不清楚。希望有知道原理的朋友留个言。