【问题标题】:Deleting a row from a dynamic HTML table从动态 HTML 表中删除一行
【发布时间】:2016-07-09 10:33:40
【问题描述】:

我想通过 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);
}

我想解决问题。谁能帮帮我?

【问题讨论】:

  • 你需要知道你的代码到底在哪里不工作。它从服务器端得到答案吗?它只是不删除行还是什么?什么不工作?
  • 在服务器端它不起作用..你只是在回答中解释我的细节,以便我能理解..
  • 我认为问题出在 jquery 代码中。这里的错误数据:id,显示它将直接调用错误:函数(dt){警报(response.status +''+响应.statusText); }
  • 所以你没有从服务器端得到答案?服务器端脚本是否与包含 javascript 的文件位于同一目录中?
  • 我知道它的库,但语言是 javascript

标签: jquery asp.net webforms


【解决方案1】:

当您创建类似列表时,将自定义 id 属性添加到行 (tr)

<tr data-id='1'></tr>
<tr data-id='2'></tr>

然后你可以换行

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 2016-09-27
    • 1970-01-01
    • 2021-12-09
    • 2010-11-05
    • 2018-04-12
    • 1970-01-01
    相关资源
    最近更新 更多