【问题标题】:ajaxForm submission happens twiceajaxForm 提交发生两次
【发布时间】:2016-05-15 04:41:45
【问题描述】:

//js

$("#saveMerchantsForm").ajaxForm({
                success : function(response){
                    alert(response);
                }
            }).submit();

//html

    <form class="form-horizontal" th:action="@{/manager/saveMerchant}" th:object="${merchantModal}" id="saveMerchantsForm" method="POST">

<div class="row">
<div class="input-field col m6 s12">
<input class="clear" th:field="*{merchantFname}" id="merchantFname" type="text"/>
<label class="merchantFname clear" style="width: 100%;" for="merchantFname">Merchant First Name</label>
</div>
<div class="input-field col m6 s12">
<input class="clear" id="merchantMname" th:field="*{merchantMname}" type="text"/>
<label class="merchantMname clear" style="width: 100%;"  for="merchantMname">Merchant Middle Name</label>
</div>
</div>
</form>

当我提交时,它提交了两次表单,我该如何防止这种情况?

【问题讨论】:

    标签: spring-mvc ajaxform


    【解决方案1】:

    您可以尝试在 .ajaxForm 函数之前添加一个取消绑定,如下所示:

    $("#saveMerchantsForm").unbind('ajaxForm');
    $("#saveMerchantsForm").ajaxForm({
                success : function(response){
                    alert(response);
                }
            }).submit();
    

    或者你可以使用这样的:

    $("#saveMerchantsForm").one('ajaxForm');
    $("#saveMerchantsForm").ajaxForm({
                success : function(response){
                    alert(response);
                }
            }).submit();
    

    one 表示这个函数每个元素只运行一次。

    unbind 移除之前绑定的元素。

    这至少解决了我的函数多次触发的问题

    【讨论】:

      猜你喜欢
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 2020-11-24
      • 1970-01-01
      • 2016-06-29
      • 1970-01-01
      • 2012-08-24
      • 2015-11-15
      相关资源
      最近更新 更多