0

I want to disable the link before ajax call and enable the link right after the response of the ajax. This is what I am trying, but it is not working:

jQuery(this).prop('disabled', false);

This is my link click event in jQuery:

jQuery("#save").click(function (e) {

    e.preventDefault();
    var pcid = jQuery(this).data('pcid');
    var id = jQuery(this).data('id');
    jQuery(this).prop('disabled', true);

    var state = jQuery('.state').html();

    jQuery.post(ajaxurl, data, function (response) {
        console.log(response.status);
        jQuery(this).prop('disabled', false);

    });
});

Everything is working correctly but the link is not been disabled and enabled after link clicked and ajax response.

This is the HTML of the tag:

<a id="save" href="#">SAVE</a>
squaleLis
  • 6,116
  • 2
  • 22
  • 30
Tayyab Vohra
  • 1,512
  • 3
  • 22
  • 49

1 Answers1

1

at the moment the button clicks, you can try to disable it.

$("#save").click(function(e) {
  $(this).attr('disabled','disabled');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="submit" id="save">SAVE</button>

When you get positive or negative results, you should use the selector instead of "this".

$("#save").removeAttr('disabled');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="submit" id="save">SAVE</button>
selçuk doğan
  • 406
  • 4
  • 12