如何在 jQuery json 中从 html 表中删除当前行

本文关键字:删除 html jQuery json 中从 | 更新日期: 2023-09-27 17:56:14

我想使用 jquery json 为multipledelete编写代码。

这是jquery代码:

function DeleteSelected() {
         var categories = new Array();
         debugger;
         // iterate all checkboxes and obtain their checked values, unchecked values are not pushed into array
         $("input[type='checkbox']").each(function ()
             //$('input[type=checkbox]').prop("checked", this.checked)
         {
             this.checked ? categories.push($(this).val()) : null;
         });
         // assume urldata is your web method to delete multiple records
         var urldata = "WebForm5.aspx/deleteRecord";
         debugger;
         $.ajax({
             type: "POST",
             contentType: "application/json; charset=utf-8",
             url: urldata,
             data: "{ 'Id':'"+JSON.stringify( categories)+"' }", // used to convert array into proper JSON format
             dataType: "json",
             success: function (dt) {
                 // done whatever tasks if succeeded
                 alert("entry deleted");
                 debugger;
                 $("#example1").DataTable();
                 //$("#example1").bind;
                 debugger;
             },
             error: function (result) {
                 alert("Error");
             }
         });
     }

这是 aspx 代码(用于多个删除的代码隐藏):

[WebMethod]
    // note clear difference between single and multiple selections
    public static void deleteRecord(List<int> Id)
    {
        clsCategoryBL objproject = new clsCategoryBL();
        // iterate through input list and pass to process method
        for (int i = 0; i < Id.Count; i++)
        {
            objproject.CategoryDelete(Id[i]);
        }
    }

我想将 id 传递给 aspx 页面,但问题是输出出现错误弹出。

 error: function (result) {
                 alert("Error");
}

如何在 jQuery json 中从 html 表中删除当前行

以下行将返回数组的 JSON categories

"{ 'Id':'"+JSON.stringify( categories)+"' }"

使用 http://jsonlint.com/验证值,并检查结果是否为有效的 JSON。

由于您已将该方法声明为 public static void deleteRecord(List<int> Id)这意味着预期的输入将类似于 [1,2,3]

还可以使用下面的行来获取确切的错误

error: function (xhr, ajaxOptions, thrownError) {
                alert("error # " + xhr + ajaxOptions + thrownError);
            }

更新 1:

下面是工作代码

var categories = new Array();
// do your logic to push
categories.push(1);
categories.push(2);
categories.push(3);
var valuetxt = "{ '"Id'":'"" + JSON.stringify(categories) + "'" }";
var urldata = "WebForm1.aspx/deleteRecord";
$.ajax({
    type: "POST",
    url: urldata,
    data: valuetxt, // used to convert array into proper JSON format
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) {
        // in result you will get same data which is posted
        alert(result.d);
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert("error # " + xhr + ajaxOptions + thrownError);
    }
});

[WebMethod]
// change the parameter to string, convert back  JSON to Integer Array
public static string deleteRecord(string Id)
{
    // do something - for sample here returning same JSON of integer
    return Id;
}