Disable submit button ONLY after submit

前端 未结 13 465
囚心锁ツ
囚心锁ツ 2020-12-02 22:49

I have the following HTML and jquery:






Test disabling submit button fo

相关标签:
13条回答
  • 2020-12-02 23:26

    Add the disable part in the submit event.

    $(document).ready(function () {
        $("#yourFormId").submit(function () {
            $(".submitBtn").attr("disabled", true);
            return true;
        });
    });
    
    0 讨论(0)
  • 2020-12-02 23:28

    Hey this works,

       $(function(){
         $(".submitBtn").click(function () {
           $(".submitBtn").attr("disabled", true);
           $('#yourFormId').submit();
         });
       });
    
    0 讨论(0)
  • 2020-12-02 23:28

    As a number of people have pointed out, disabling the submit button has some negative side effects (at least in Chrome it prevents the name/value of the button pressed from being submitted). My solution was to simply add an attribute to indicate that submit has been requested, and then check for the presence of this attribute on every submit. Because I'm using the submit function, this is only called after all HTML 5 validation is successful. Here is my code:

      $("form.myform").submit(function (e) {
        // Check if we have submitted before
        if ( $("#submit-btn").attr('attempted') == 'true' ) {
          //stop submitting the form because we have already clicked submit.
          e.preventDefault();
        }
        else {
          $("#submit-btn").attr("attempted", 'true');
        }
      });
    
    0 讨论(0)
  • 2020-12-02 23:29

    Test with a setTimeout, that worked for me and I could submit my form, refers to this answer https://stackoverflow.com/a/779785/5510314

    $(document).ready(function () {
        $("#btnSubmit").click(function () {
            setTimeout(function () { disableButton(); }, 0);
        });
    
        function disableButton() {
            $("#btnSubmit").prop('disabled', true);
        }
    });
    
    0 讨论(0)
  • 2020-12-02 23:32

    This solution has the advantages of working on mobile and being quite simple:

    <form ... onsubmit="myButtonValue.disabled = true; return true;">
    
    0 讨论(0)
  • 2020-12-02 23:34

    I put this in my global code to work on all submit buttons:

    $("input[type='submit']").on("click", function (e) {
        $(this).attr("disabled", true);
        $(this).closest("form").submit()
    });
    
    0 讨论(0)
提交回复
热议问题