从动态HTML表中删除行

时间:2016-07-09 10:33:41

标签: jquery asp.net webforms

我想通过发出json请求来删除行,但遗憾的是代码无效。

function deleteRecord(id) {
    debugger;
    // === Show confirmation alert to user before delete a record.
    var ans = confirm("Are you sure to delete a record?");

    // === If user pressed Ok then delete the record else do nothing.
    if (ans == true) {
        debugger;
        $.ajax({
            type: "POST",
            url: location.pathname + "WebForm5.aspx/deleteRecord",
            data: id,
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            async: "true",
            success: function (dt) {
                debugger;

                //=== rebind data to remove delete record from the table.
                $(this).closest('tr').remove();
                $(".errMsg ul").remove();
                $(".errMsg").append("<ul><li>Record successfully delete.</li></ul>");
                $(".errMsg").show("slow");
                clear();
            },
            error: function (dt) {
                alert(response.status + ' ' + response.statusText);
            }
        });
    }
}

这是我在ASP.net中的aspx.cs代码后面的代码:

public static void deleteRecord(int CategoryID)
{
    clsCategoryBL objproject = new clsCategoryBL();
    objproject.CategoryDelete(CategoryID);
}

我想解决问题。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

在创建像

这样的列表时,将自定义ID属性添加到行(tr)
<tr data-id='1'></tr>
<tr data-id='2'></tr>

然后你可以换线

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

$('tr[data-id='+id+']').remove();