I am doing some AJAX when a button is clicked
$btn.button('loading');
$.ajax({
type: 'PUT',
url: 'event/',
data: put_data,
dataType: 'json',
success: function(data){
if (data.redirect) {
window.location = data.redirect;
}
else {
$btn.button('reset');
if ($btn.is('#my-btn')) {
console.log('disabling button');
$btn.prop('disabled', true);
}
}
}
});
disabling button
shows up in the console, but the button does not get disabled. Loading and resetting the button works just fine. I tried .addClass('disabled')
and .attr('disabled','disabled')
, but those also don’t work.
See here: http://codepen.io/anon/pen/Wvbeeg
3
Answers
Remove:
For some reason, this doesn’t evaluate until after you disable it. (It’s asynchronous)
Rather than resetting the button using the ‘reset’ code, it might be better to just remove the
disabled
property if it exists. That will effectively enable the button if it should be active.Modified code:
Bonus tip: Don’t use .removeProp() to re-enable the button. That will completely remove the property and you won’t be able to use it again. More about prop() here: https://api.jquery.com/prop/
The problem is that
button('reset')
is asynchronous. You can watch the changes live in the html an see they aren’t immediateA short delay after reset resolves the problem, but personallly i would just code this whole process myself instead of using the bootstrap API
DEMO
It’s because you are using jQuery to bind a click event to the button. So the disable property won’t be able to stop it. In your code you will need to add this:
That will unbind the event from the button.