[Django]-Disable button after submit with jQuery

106๐Ÿ‘

โœ…

Try this:

$('form').submit(function() {
  $(this).find("button[type='submit']").prop('disabled',true);
});

29๐Ÿ‘

I like this, donโ€™t have to traverse the DOM.
Put function on a setTimeout function, this allows make submit and after disable button, even if setTimeout is 0

$(document).ready(function () {
$("#btnSubmit").click(function () {
    setTimeout(function () { disableButton(); }, 0);
});

function disableButton() {
    $("#btnSubmit").prop('disabled', true);
}
});

8๐Ÿ‘

You could disable it upon the parent formโ€™s submit event:

$("form").on("submit", function () {
    $(this).find(":submit").prop("disabled", true);
});

Be sure to run this code only after the HTMLFormElement has been loaded, or else nothing will be bound to it. To ensure that the binding takes place, fire this off from within a document-ready block:

// When the document is ready, call setup
$(document).ready(setup);

function setup () {
    $("form").on("submit", function () {
        $(this).find(":submit").prop("disabled", true);
    });
}

5๐Ÿ‘

Try, like this,

  <input type="submit" value="Send" onclick="javascript=this.disabled = true; form.submit();">

5๐Ÿ‘

Something like this might work.

<button id="btnSubmit" type='submit'> Send </button>

<script>
     $("#btnSubmit").on("click", function(e){
          e.PreventDefault();
          $(this).closest("form")[0].submit();
          $(this).prop('disabled',true)
     });
</script>

3๐Ÿ‘

This ended up being the best solution for me

$("form").submit(function disableSubmit() {
  $("input[type=submit]", this).prop("disabled", true);
});

2๐Ÿ‘

my variant, disable button, no direct disabled but only vidible hidden:

<input type="submit" name="namebutton" value="Nahrรกt obrรกzek"  onclick="this.style.visibility='hidden';" ondblclick="this.style.visibility='hidden';"/>

2๐Ÿ‘

You can do something like this. It is work fine with me.

<form method='post' onSubmit='disableFunction()'>
// your code here
</form>

Then in script, add this

<script>
function disableFunction() {
    $('#btn_submit').prop('disabled', true);
}
</script>

1๐Ÿ‘

How about this?

onclick="this.style.visibility='hidden';"

I would say, instead of disabled, hide it.

If you want to go with disabled

onclick="this.style.disabled='true';"

1๐Ÿ‘

Got an issue on Chrome, wasnโ€™t submitting the form. Tried a bunch of different code, this was what worked best for me (and looks best imo):

  $('#form_id').submit(function() {
    $("input[type='submit']", this)
      .val("Please Wait...")
      .attr('disabled', 'disabled');
    return true;
  });

Replace form_id with the id of your form. Classes work too of course: $('.form_class')

Source: JavaScript Coder

0๐Ÿ‘

I like this better:

<script>
    var submit = false;
    $('form').submit(function () {
        if (submit) { return false; }
        else { submit = true;}
    });
</script>

this way it also prevents the enter key to submit more than once

0๐Ÿ‘

Iโ€™m using Chrome. Any idea on why I have this problem?

Well, first time I dealt with this, I solved it like this:

function blockButtons() {
   $('button:submit').click(function(){
    $('button:submit').attr("disabled", true);
   });
}

This worked perfectly, butโ€ฆ in Mozilla Firefox. The Google Chrome did not submit it, so I changed it to this:

function blockButtons() {
   $('button:submit').click(function(){
      var form = $(this).parents('form:first');
    $('button:submit').attr("disabled", true);
    $('button:submit').css('opacity', 0.5);
    form.submit();
   });
}

This worked both in Mozilla Firefox, however, after that some of our users using old versions of IE experienced trouble of having two submits. That is, the one initiated by the javascript, and the one by browser ignoring the fact of onclick and just submitting anyway. This can be fixed by e.preventDefault, I guess.

0๐Ÿ‘

If you donโ€™t want an element to be double-clicked, use .one()

<button id="submit" type="submit">Send</button>
<script>
$(function () {
$("#submit").one("click", function () {
//enter your submit code
 });
});

.one()

0๐Ÿ‘

You can do something like this. It is work fine with me.

$("button#submitted").click(function () {
        $("button#submitted").prop('disabled', true);
});

Double click on your button. This code will running

0๐Ÿ‘

You must prevent the form from being submitted more than once, disabling the button is not the right solution because the form could be submitted in other ways.

JavaScript:

$('form').submit(function(e) {
    // if the form is disabled don't allow submit
    if ($(this).hasClass('disabled')) {
        e.preventDefault();
        return;
    }
    $(this).addClass('disabled');
});

Once the form is correctly disabled, you can customize its appearance.

CSS:

form.disabled {
    pointer-events: none;
    opacity: 0.7;
}

Leave a comment