0

我想使用 Jquery 提交多个表单(已经完成)但问题是..每次提交后我希望表单从列表表单中删除这是我的 JSP 代码

<form action="searchTweet" method="post">
    <textarea name="searchTxt"></textarea>
    <input type="submit" name="post" value="Search"/>

</form>
 <%
    if(session.getAttribute("twitModel")!=null)
    {

       List<TwitterModel> twit=(List<TwitterModel>)session.getAttribute("twitModel");

       int count=0;
       for(TwitterModel tweet:twit)
    {
           count=count+1;

%>
<p>
    @<%=tweet.getScreenName()%> : <%=tweet.getTweet() %>
   <form id="form2" method="post">
   <input type="hidden"  name="screenName" value="<%= tweet.getScreenName() %>">
   <input type="hidden"  name="tweet" value="<%= tweet.getTweet() %>">
   <input type="submit"  class="update_form" value="Save"> <!-- changed -->
   </form>
    </p>     <%   } }  %>

我的多个提交表单的 Jquery

   <script>
   // this is the class of the submit button
   $(".update_form").click(function() { // changed
    $.ajax({
       type: "GET",
       url: "saveSearch",
       data: $(this).parent().serialize(), // changed
       success: function(data)
       {
         $(this).parents('p').remove();
       }
     });
      return false; // avoid to execute the actual submit of the form.
        });

知道我在做什么错吗?

谢谢

问候

丹兹

4

2 回答 2

1

我猜你this已经更改为函数window内部的全局变量success,试试这个:

 $(".update_form").click(function() { // changed
    var me = this;
    $.ajax({
       type: "GET",
       url: "saveSearch",
       data: $(this).parent().serialize(), // changed
       success: function(data)
       {
         $(me).parents('p').remove();
       }
     });
      return false; // avoid to execute the actual submit of the form.
   });

阅读闭包

于 2013-11-07T07:29:28.690 回答
0

this成功处理程序内部指的是 ajax 对象,而不是单击的按钮。在这种情况下,您可以使用闭包变量来解决问题。

也不使用 .closest() 而不是 .parents()

$(".update_form").click(function () { // changed
    var $this = $(this);
    $.ajax({
        type: "GET",
        url: "saveSearch",
        data: $(this).parent().serialize(), // changed
        success: function (data) {
            $this.closest('p').remove();
        }
    });
    return false; // avoid to execute the actual submit of the form.
});
于 2013-11-07T07:29:46.683 回答