gpt4 book ai didi

jquery - 如何使用ajax删除表中的一行

转载 作者:行者123 更新时间:2023-12-01 05:30:54 26 4
gpt4 key购买 nike

在此部分 View 中,我使用模型和 foreach 循环填充表格,如下所示:

<table id="tblGroup" class="table table-bordered">

<thead style="background: #eeeeee;">
<tr>
<th style="text-align: center;">ردیف</th>
<th style="text-align: center;">نام</th>
<th style="text-align: center;">تخصص</th>
<th style="text-align: center;">سابقه</th>
<th style="text-align: center;">حذف</th>
</tr>
</thead>
<tbody style="text-align: center;">
@foreach (var item in Model)
{
<input type="hidden" value="@item.Id" id="groupId"/>



<tr id="idrow">


<td >@(orderCounter++)</td>
<td>@blSpecialist.Find(item.Sp_id).Name @blSpecialist.Find(item.Sp_id).Family</td>
<td>@blSpecialist.Find(item.Sp_id).Specially</td>
<td>@blSpecialist.Find(item.Sp_id).WorkYear</td>
<td><a href="#" id ="linkdelete"><i class="icon-remove"></i></a></td>

</tr>


}
</tbody>

我想在单击删除链接时删除一行。这是我的 JQuery,但它不起作用:

<script type="text/javascript">
$("#linkdelete").click(function (e) {
$.ajax({
url: "/Admin/DeleteGroup",
data: { id: $('input#groupId').val() },
//cache: false,
//contentType: false,
//processData: false,
//mimeType: "multipart/form-data",
type: "Post",
dataType: "Json",
success: function(result) {
if (result.Success) {
$("#idrow").remove();
}
eval(result.Script);
},
error: function() {
alert("خطا!");
}
});
});

请帮我做到这一点。

最佳答案

向 anchor 标记添加 data-id 属性:

<a href="#" data-id="@item.Id" id ="linkdelete"><i class="icon-remove"></i></a>

您可以像这样删除行:

$("#linkdelete").click(function (e) {
var obj = $(this); // first store $(this) in obj
var id = $(this).data('id'); // get id of data using this
$.ajax({
url: "/Admin/DeleteGroup",
data: { id: id },
//cache: false,
//contentType: false,
//processData: false,
//mimeType: "multipart/form-data",
type: "Post",
dataType: "Json",
success: function(result) {
if (result.Success) {
$(obj).closest("tr").remove(); // You can remove row like this
}
eval(result.Script);
},
error: function() {
alert("خطا!");
}
});
});

关于jquery - 如何使用ajax删除表中的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37324017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com