【问题标题】:Remove Form After Submit Jquery (JSP&Servlet)提交 Jquery (JSP&Servlet) 后删除表单
【发布时间】:2013-11-18 18:12:16
【问题描述】:

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

知道我做错了什么吗??

谢谢

问候

舞蹈

【问题讨论】:

    标签: javascript jquery forms jsp servlets


    【解决方案1】:

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

     $(".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.
       });
    

    阅读closures

    【讨论】:

    • 我已经尝试过您的解决方案但仍然没有运气,它只提交表单但没有从页面中删除提交的表单..
    • 你确定success函数被调用了吗?尝试使用 complete 而不是 success 选项,看看你得到了什么 responseCode?​​span>
    • 使用警报我打印出数据..如果我使用完整选项它会打印出 xmlhttpRequest,如果我使用成功它会显示来自我的 servlet 的响应..
    • 那么在服务器端没问题..按照 Arun 的建议,closest 工作了吗?尝试$('#form2').remove(),如果是,则替换为$('#form2').parent().remove()
    • 好的!非常感谢:)。就像你向我建议的那样,我正在使用 $('#form2').remove();
    【解决方案2】:

    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.
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 2012-01-31
      • 2019-04-21
      相关资源
      最近更新 更多