2012-03-07 70 views
1

如何通過在鏈接按鈕上使用jquery ajax調用傳遞id來刪除一行?jquery ajax呼籲刪除一行

$(document).ready(function(){ 
    $(".remove_row").click(function (event){ 
     event.preventDefault();   
     var url = $(this).attr('href'); 
     $.ajax({ 
      url: url, 
      success: function() { return false; }, 
      error: function(){ alert('An error.'); } 
     }); 
     return false; 
    ); 
}); 

我是否需要提及dataType:和write成功函數..當我想刪除行時?

+0

你收到你的頁面調用?在'url'變量中顯式的頁面?如果是的話,那真的是你的問題? – balexandre 2012-03-07 07:58:49

+0

什麼是'.remove_row'?你的桌子是怎樣的?用你的ajax調用覆蓋你的刪除按鈕的正常功能。所以你不能觸發正常的刪除了。爲了幫助您,我們需要更多信息。 – PiTheNumber 2012-03-07 08:04:42

回答

1

你的問題是不明確的,假設這些可能的情況:

  1. 要刪除您的網頁上的任何元素

    $('#Elementid').remove(); 
    
  2. 你想從數據綁定列表中刪除一行
  3. 或表即你想從你的數據庫中刪除行:

    在這種情況下,你將需要調用服務器端的靜態方法,並傳遞要刪除的行的id可以做如下圖所示:

    $.ajax({ 
          type: "POST", 
          url: "WebForm1.aspx/ServerSideMethod", 
          data: "{'id':value}", 
          contentType: "application/json; charset=utf-8", 
          dataType: "json", 
          async: true, 
          cache: false, 
          success: function (msg) { 
           $('#myDiv').text(msg.d); 
          } 
    }); 
    

和你的WebForm1.aspx的ServerSideMethod應該是這樣的:

[WebMethod] 
    public static string ServerSideMethod(int id) 
    { 
     string query ="delete from table where id="+id; 
     //rest of the dml operation 
     return "Message from server."; 
    } 
+0

對不起,問題不清楚。我試圖在循環中刪除

。 ajax調用是從數據庫中刪除該id,但該行未被刪除。所以我做了什麼是給了一個ID,收到了ID作爲迴應,並動態刪除了特定的
行。感謝您的回覆。 – user430266 2012-03-08 23:51:45

0

刪除任何HTML元素的使用:

$('#row_id').remove(); 

這可能不是一個完整的解決方案,但我認爲這個問題是不完整的,所以這是我能做到的最好。