Disable then enable jQuery click function again

I have a file upload system, after clicking the download button, the file is then downloaded via AJAX. While the file is uploaded, I want to disable the click function located on the "Select Images" button. This is currently the function of clicking on the file select button:

$(document).ready(function() {
    $("#file-button").click(function() {
        $('#file').trigger('click');
    });
});

This works fine, but I want to turn off the click function at runtime of XmlHttpRequest, and then turn on the click function again when I get a response from server 200. I tried bind()and unbind()and it works fine in Chrome, but in firefox the button cannot be on load I clicked what I want, and then after receiving a response from the server, the button is repeated, but in firefox, two file selection dialogs open simultaneously. This is because of the function above, and I bind the function again using bind(). Does anyone have any suggestions for enabling, then turn off the button without re-entering the code (function) of the click event.

Something like this would be preferable:

$('#file-button').disable();
$('#file-button').enable();

I tried on()and off()and they don't seem to work either.

+5
source share
4

-

:

$(document).ready(function() {
    $("#file-button").click(function() {
      if ( $('#file-button').attr('disabled') == "disabled" ) {
        return false;
      }
      else {
          $('#file').trigger('click');
      }
    });
});

,

$('#file-button').attr('disabled','disabled');

:

$('#file-button').removeAttr('disabled');
+7

, jQuery $. prop():

$("input[type=submit]").prop('disabled', true);

, , :

$("#file-button").click(function() {
  if (!$(this).is(':disabled')) {
    $('#file').trigger('click');
  }
});

:

$("input[type=submit]").prop('disabled', false);

, :

$("#whatever-your-form-is").on('submit', function() {
  $('#file').trigger('click');
});
+3

Try Attrthe jQuery function.

$('#file-button').attr('disabled','disabled');
$('#file-button').removeAttr('disabled');

Verified code

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">

    $(document).ready(function()
    {
        $("#file-button").click(function(e)
        {
            e.preventDefault();
            $(this).attr('disabled','disabled');
            return false;
        });
    });

</script>

<input type="button" id="file-button" value="ClickMe" />
0
source

You need to go to the enter button to disable the button,

Something like below

  $("input[type=submit]").prob('disabled', true);
  $("inpur[type=submit]").prob('disabled', false);
0
source

All Articles