2012-03-07 24 views
-1

表内删除的行我有以下看法: -如何删除使用jQuery我的asp.net MVC

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#removerow").click(function() { 
      $(this).remove(); 
     }); 
    }); 
</script> 


<h2>Details</h2> 

<fieldset> 
    <legend>This Question Currently has @Model.Answers.Count() Answers/s. </legend> 
<table id="incrementanswer"> 
    <tr> 
     <th> 
      Description 
     </th> 
     <th> 
      Answer 
     </th> 
     <th></th> 
    </tr> 

@foreach (var answer in Model.Answers.OrderBy(a=> a.IsRight)) 
{ 
     <tr id = "removerow"> 

     <td> 
      @Html.DisplayFor(modelItem => answer.Description) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => answer.Answer_Description.description) 
     </td> 
     <td> 
     @Ajax.ActionLink("Delete", "Delete", "Answer", 
     new { id = answer.AnswersID }, 
      new AjaxOptions 
      { 
       Confirm = "Are You sure You want to delete this Answer ?", 
       HttpMethod = "Post", 
       UpdateTargetId = "toremove", 
       OnSuccess = "removePartial2" 
      }) 


     </td> 
     </tr> 
} 
</table> 
<div id = "progress2"> 
<img src= "@Url.Content("~/Content/images/ajax-loader1.gif") "> 
</div> 

其中包含脚本和ajax.actionlink在点击后调用此脚本在删除链接上。目前,只有当它是表中的第一行时,该行才会被删除,并且在确认删除之前该行将被删除,那么可能是什么问题?

编辑: - 我我的观点已经更新到了下面,但我仍然不能强迫removePartial2脚本删除其删除的行: - 。

</script> 
<script type="text/javascript"> 
    function removePartial2() { 
     $(this).remove(); 
} 
</script> 

} 

<h2>Details</h2> 

<fieldset> 
    <legend>This Question Currently has @Model.Answers.Count() Answers/s. </legend> 
<table id="incrementanswer"> 
    <tr> 
     <th> 
      Description 
     </th> 
     <th> 
      Answer 
     </th> 
     <th></th> 
    </tr> 

@foreach (var answer in Model.Answers.OrderBy(a=> a.IsRight)) 
{ 
     <tr id = @answer.AnswersID> 

     <td> 
      @Html.DisplayFor(modelItem => answer.Description) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => answer.Answer_Description.description) 
     </td> 
     <td> 
     @Ajax.ActionLink("Delete", "Delete", "Answer", 
     new { id = answer.AnswersID }, 
      new AjaxOptions 
      { 
       Confirm = "Are You sure You want to delete this Answer ?", 
       HttpMethod = "Post", 
       UpdateTargetId = @answer.AnswersID.ToString(), 
       OnSuccess = "removePartial2" 
      }) 


     </td> 
     </tr> 
} 
</table> 
<div id = "progress2"> 
<img src= "@Url.Content("~/Content/images/ajax-loader1.gif") "> 
</div> 
+1

ID。必须。是。独特。 – 2012-03-07 15:11:13

+0

什么是您的removePartial2中的$(this)?你确定它引用了你想要的对象吗? – Rastapopulous 2012-03-07 22:05:33

+0

我想强制应用程序删除预期的行,那为什么我使用$(this);但似乎它不工作...... – 2012-03-07 22:51:59

回答

0
  1. “ID必须。独一无二“。

  2. 你有一个函数可以在单击ROW的时候删除行,而不仅仅是删除选项。我会删除你的第一个JavaScript块,并只使用ajax调用函数。

  3. OnSuccess是Ajax调用是否返回200 code。只有在ajax调用成功的情况下 - 这不是检查返回的值是否正确。

+0

我已编辑我的帖子,使ID独特... – 2012-03-07 16:42:37

+0

@johnG我已经更新了我的答案。如果您的ajax调用成功,您将删除该行。这意味着来自服务器的200返回代码。 – Rastapopulous 2012-03-07 20:54:51