禁用然后重新启用click function jQuery [英] Disable then re-enable click function jQuery

查看:105
本文介绍了禁用然后重新启用click function jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件上传系统,点击上传按钮后,文件会通过AJAX上传。在上传文件时,我想禁用选择图像按钮上的单击功能。
目前这是文件选择按钮上的点击功能:

I have a file upload system, after the upload button is clicked, the file is then uploaded through AJAX. While the file is uploaded I want to disable the click function that is on the "Select Images" button. Currently this is the click function on the file-selection button:

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

工作正常,但我想在XmlHttpRequest的进度阶段禁用click函数,并且然后当我从服务器得到200响应时重新启用click功能。我尝试过 bind() unbind(),它在Chrome中工作正常,但在firefox中,在上传过程中,按钮无法点击,这就是我想要的,然后我从服务器得到响应按钮重新启用,但在firefox中同时打开两个文件选择对话窗口。这是因为上面的函数,我使用 bind()再次绑定函数。有没有人对如何启用有任何建议,然后禁用该按钮而不重新输入点击事件的代码(功能)。

That works fine, but I want to disable the click function in the progress phase of the XmlHttpRequest, and then re-enable the click function when I get a 200 response from the server. I have tried bind() and unbind() and it works fine in Chrome, but in firefox, during the upload, the button cannot be clicked, which is what I want, and then after I get a response from the server the button is re-enabled, but in firefox two file-selection dialogue windows open at the same time. This is because of the function above, and me binding the function again using bind(). Does anyone have any suggestions on how to enable, then disable the button without re-entering the code (function) of the click event.

这样的事情会更好:

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

我在()上尝试过 off(),它们似乎也无效。

I have tried the on() and off() and they do not seem to work either.

推荐答案

解决方案 - 感谢Eric

SOLUTION -- thanks to Eric

我将初始点击功能更改为以下内容:

I changed my initial click function to the following:

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

我设置以下内容以禁用按钮

And I set the following to disable the button

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

这是重新启用它:

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

这篇关于禁用然后重新启用click function jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆