首页 > 解决方案 > 表单提交两次

问题描述

我有一个 asp.net MVC 应用程序。

下面的 javascript 代码自 10 天前开始投入生产。这个想法是在允许提交表单之前拦截提交事件并调用 ajax 事件来检查附件大小。

问题:我们报告了 2 个用户提交了两次表单。我不知道这怎么可能。也许使用较旧的 IE 版本?

$(function () {

   $("input[type=submit]").bind("click", function (e) {

      e.preventDefault();
      e.returnValue = false;

      var $form = $(this);

      $.ajax({
        type: 'post',
        url: pMailPreview.checkAttachmentsSizeExceedUrl,
        cache: false,
        context: $form,
        success: function (result, textStatus, XMLHttpRequest) {
            if (result.sizeExceed) {
                alert('Size exceed !!');
            } else {
                // make sure that you are no longer handling the submit event; clear handler
                this.off('submit');
                // actually submit the form
                this.submit();
            }
        },
        error: function (jqXhr, textStatus, errorThrown) {
            alert("Error '" + jqXhr.status + "' (textStatus: '" + textStatus + "', errorThrown: '" + errorThrown + "')");
        },
      });

      return false;

  });

});

标签: javascriptasp.net-mvc

解决方案


如果用户在按钮上单击两次并且表单有效,它将被发送两次。

您可以禁用提交按钮。


推荐阅读