【问题标题】:Disable buttons until the task is finished jQuery?禁用按钮直到任务完成jQuery?
【发布时间】:2014-02-14 05:46:06
【问题描述】:

我已经在一些像这样的 li 元素上应用了点击事件

jQuery('ul li').click(function(){ doSomething(); });

现在我有一个要求,直到 doSomething 完成。我希望其他 li 元素除了被单击的元素被禁用并且不允许单击。 doSomething 基本上是一个 ajax 调用。

【问题讨论】:

标签: javascript jquery ajax


【解决方案1】:

我会尝试这样的:

var lock = false;

jQuery('ul li').click(function(){
   if(lock == true){
      return false; 
   }
   lock = true;
   doSomething();

});

doSomething 完成后设置lock = false;

例如:

success: function(){
   // your ajax success code 
   lock = false;
}

【讨论】:

    【解决方案2】:

    您可以使用标志和 ajax 完成回调

    var doing = false;
    jQuery('ul li').click(function () {
        if (doing) {
            return;
        }
        doing = true;
        doSomething(function () {
            doing = false;
        });
    });
    
    function doSomething(complete) {
        $.ajax().always(complete)
    }
    

    【讨论】:

      【解决方案3】:

      答案:

      $(function(){
         $("ul.a li").click(function(){
           $(this).show();//disable true condition write here
           $(this).siblings().hide();//disable false condition write here
      });
      });
      

      【讨论】:

      • 我想我的问题是如何禁用 li 元素。它不是按钮,它仍然可以点击。那么我如何解除与其他 li 的事件的绑定
      猜你喜欢
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      相关资源
      最近更新 更多