【问题标题】:How to remove current row from a html table in jQuery json?如何从 jQuery json 中的 html 表中删除当前行?
【发布时间】:2016-11-13 07:44:09
【问题描述】:

我想使用 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");
}

【问题讨论】:

  • 尝试将alert("Error") 替换为alert(result) 以查看错误所在。
  • 错误是[object][Object]
  • 使用console.log() 而不是alert()
  • 我想将 id 传递给我的 aspx 页面,但它 terurns null 显示它会返回 false,所以你能告诉我如何传递 id 吗?如果我使用 console.log() 没有弹出窗口..那么如何解决?
  • 看起来像语法错误尝试用'{"ID":"' + JSON.stringify( categories)+ '"}';替换"{ 'Id':'"+JSON.stringify( categories)+"' }"

标签: c# jquery html asp.net json


【解决方案1】:

下面的行将返回数组categories的JSON

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

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

由于您已将方法声明为 public static void deleteRecord(List&lt;int&gt; 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 2019-12-17
    相关资源
    最近更新 更多