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);
}
});
- [Django]-How can I call a custom Django manage.py command directly from a test driver?
- [Django]-Use Python standard logging in Celery
- [Django]-Writing a __init__ function to be used in django model
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);
});
}
- [Django]-Django Rest Framework โ no module named rest_framework
- [Django]-How to disable Django's invalid HTTP_HOST error?
- [Django]-Django Rest Framework remove csrf
5
Try, like this,
<input type="submit" value="Send" onclick="javascript=this.disabled = true; form.submit();">
- [Django]-Django URLs TypeError: view must be a callable or a list/tuple in the case of include()
- [Django]-Django character set with MySQL weirdness
- [Django]-Django rest framework nested self-referential objects
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>
- [Django]-Cancel an already executing task with Celery?
- [Django]-Where to store secret keys DJANGO
- [Django]-Invalid http_host header
3
This ended up being the best solution for me
$("form").submit(function disableSubmit() {
$("input[type=submit]", this).prop("disabled", true);
});
- [Django]-Django development server reload takes too long
- [Django]-Iterate over model instance field names and values in template
- [Django]-How do I create sub-applications in Django?
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';"/>
- [Django]-Docker/Kubernetes + Gunicorn/Celery โ Multiple Workers vs Replicas?
- [Django]-Empty Label ChoiceField Django
- [Django]-Row level permissions in django
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>
- [Django]-Speeding up Django Testing
- [Django]-Is there a built-in login template in Django?
- [Django]-How to show a many-to-many field with "list_display" in Django Admin?
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';"
- [Django]-OperationalError: database is locked
- [Django]-Effects of changing Django's SECRET_KEY
- [Django]-How to get getting base_url in django template
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
- [Django]-How to call function that takes an argument in a Django template?
- [Django]-How can I filter a date of a DateTimeField in Django?
- [Django]-What's the idiomatic Python equivalent to Django's 'regroup' template tag?
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
- [Django]-Django migration with uuid field generates duplicated values
- [Django]-Programmatically saving image to Django ImageField
- [Django]-Pip install PIL fails
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.
- [Django]-H14 error in heroku โ "no web processes running"
- [Django]-Django: Reference to an outer query may only be used in a subquery
- [Django]-Django model constraint for related objects
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
});
});
- [Django]-Identify the changed fields in django post_save signal
- [Django]-Attempt to write a readonly database โ Django w/ SELinux error
- [Django]-Can I have a Django model that has a foreign key reference to itself?
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
- [Django]-Paginate relationship in Django REST Framework?
- [Django]-How to use Python type hints with Django QuerySet?
- [Django]-PIL /JPEG Library: "decoder jpeg not available"
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;
}
- [Django]-ImportError: No module named virtualenv
- [Django]-Remove Labels in a Django Crispy Forms
- [Django]-How can one change the type of a Django model field from CharField to ForeignKey?