【问题标题】:MVC - Issue with users double-clicking Submit buttonMVC - 用户双击提交按钮的问题
【发布时间】:2016-05-06 13:32:53
【问题描述】:

我的 MVC 应用程序中有许多页面,用户单击提交按钮来发布表单。有时用户会单击提交,但由于没有立即发生,请再次单击它。因此,表单提交了两次。为了防止这种情况,我有以下 JavaScript 代码:

// When the user submits the form, disable the Save button so there is no chance
// the form can get double posted.
$('#form').submit(function () {
    $(this).find('input[type=submit]').prop('disabled', true);

    return true;
});

此代码禁用提交按钮,因此用户无法单击两次。这工作正常。但是,如果表单上存在客户端验证错误,则提交按钮将被禁用,但表单永远不会发布,现在用户无法发布表单。我可以对 JS 代码进行更改以检测是否存在客户端验证错误,如果是,我要么不禁用提交按钮,要么重新启用它?

【问题讨论】:

    标签: javascript model-view-controller


    【解决方案1】:

    如果您使用的是 jQuery Validate,您可以在禁用按钮之前检查表单是否有效:

    $('#form').submit(function () {
        if ($(this).valid()) {
            $(this).find('input[type=submit]').prop('disabled', true);
        }
    });
    

    【讨论】:

      【解决方案2】:

      你可以试试这样的:

      <button id="subButton" /> <!-- do not use type="submit" because some browsers will automatically send the form -->
      

      Javascript:

      $('#subButton').click(function (e) {
          e.preventDefault(); //prevent browser's default behaviour to submit the form
          $(this).prop('disabled', true);
          doValidation();
      });
      
      var pTimeout;
      function doValidation() {
      ajaxLoader.show(); //lock the screen with ajaxLoader
      var form = $('#registerForm');
      var isPending = form.validate().pendingRequest !== 0; // find out if there are any pending remote requests ([Remote] attribute on model)
      if (isPending) {
          if (typeof pTimeout !== "undefined") {
              clearTimeout(pTimeout);
          }
          pTimeout = setTimeout(doValidation, 200); //do the validation again until there are no pending validation requests
      }
      var isValid = form.valid(); //have to validate the form itself (because form.Valid() won't work on [Remote] attributes, thats why we need the code above
      if (!isPending) {
          ajaxLoader.hide();
          if (isValid) {
              $('#registerForm').submit(); //if there are no pending changes and the form is valid, you can send it
          }
          else {
              $('#subButton').prop('disabled', false); //else we reenable the submit button
          }
      }};
      

      【讨论】:

        【解决方案3】:

        切换它。

        $("input[type='submit']").on("click", function (event) {
           event.preventDefault();
           $(this).prop("disabled", true);
           // perform error checking
           if (noErrors) {
              $("#form").submit();
           }
           else {
              $(this).prop("disabled", false);
           }
        });
        

        【讨论】:

        • 随心所欲。你可以有一个函数来检查你的输入值,你可以使用 jquery validate,真的是你喜欢的任何东西。我通常有一个 checkFormForErrors() 函数,它根据每个字段的自定义要求验证我的所有输入
        • 验证已经通过 MVC 和我放在各个字段上的属性完成。我需要知道验证是通过还是失败,然后采取相应措施。
        • @RandyMinder 我建议使用 jQuery Validate,你可以简单地说 $("form").valid()
        • @downvoter 我能找到理由吗?这是一个完全可以接受的解决方案
        猜你喜欢
        • 2011-07-11
        • 2018-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-15
        • 2017-06-17
        • 1970-01-01
        相关资源
        最近更新 更多