I am trying to achieve the scenario of disabling a button on clicking it, and then enabling it again after the ajax request has completed successfully.
The below is the snippet of my code.
Form
<button id="btnSubmit" type="submit" class="btn btn-warning btn-lg">Submit!</button>
Javascript
$('#resForm').validate({
// disable submit button
$('#btnSubmit').prop('disabled', true);
submitHandler: function(form) {
$.ajax({
..... typical ajax stuff .....
success: function(data) {
alert(data);
$('success').html(data);
// enable reserve button again
$('#btnSubmit').prop('disabled', false);
},
error: function (jqXHR, textStatus, errorThrown) {
// console.log(jqXHR);
}
});
}
});
It doesn't work. And checking my console on Chrome tells me Uncaught SyntaxError: Unexpected token (
. It feels like it's a stupid mistake somewhere and I can't figure it out. I have read that prop
is meant for jQuery 1.6.1 and later and I am on 1.8.3.
Add data-toggle="button" to toggle a button's active state. If you're pre-toggling a button, you must manually add the . active class and aria-pressed="true" to the <button> .
The disabled attribute is a boolean attribute. When present, it specifies that the button should be disabled. A disabled button is unusable and un-clickable. The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met (like selecting a checkbox, etc.).
Make buttons look inactive by adding the disabled boolean attribute to any <button> element. Disabled buttons using the <a> element behave a bit different: <a> s don't support the disabled attribute, so you must add the .disabled class to make it visually appear disabled.
$('#resForm').validate({
submitHandler: function(form) {
// disable submit button
$('#btnSubmit').prop('disabled', true);
$.ajax({
..... typical ajax stuff .....
success: function(data) {
alert(data);
$('success').html(data);
// enable reserve button again
$('#btnSubmit').prop('disabled', false);
},
error: function (jqXHR, textStatus, errorThrown) {
// console.log(jqXHR);
}
});
}
});
Try this
From my answer to another post in SO! I can't think a simpler/easier way! ;-)
For Anchor Tags(Links) :
<a href="#delete-modal" class="btn btn-danger" id="delete"> Delete</a>
To enable the Anchor tag:
$('#delete').removeClass('disabled');
$('#delete').attr("data-toggle", "modal");
To disable the Anchor tag:
$('#delete').addClass('disabled');
$('#delete').removeAttr('data-toggle');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With