代码之家  ›  专栏  ›  技术社区  ›  harunB10

防止双击某些元素

  •  0
  • harunB10  · 技术社区  · 6 年前

    我有一个可点击的 <td>

    $.each(response, function(index) {
        $('#myID').append('<tr><td onclick="select(this)" >'+ response[index] +'</td></tr>');
    });
    
    function select(element){
    ...
    }
    

    我试着用jQuery的 .one() $(document).ready(); 在这里。据我所知,我必须使它像 onclick="select(this)" ... 而且很有效。但这里我需要禁用双击。

    2 回复  |  直到 6 年前
        1
  •  2
  •   epascarello    6 年前

    因此,添加一个检查,以确保Ajax请求是活动的。。。。

    function select(element){
      var elem = $(element);
      if(elem.hasClass("active")) {  // is ajax call active?
        return false;
      }
      elem.addClass("active");  // set it that it is active
      $.ajax({
        url: "foo"
      })
        .done(function(){})
        .always(function(){
          elem.removeClass("active");  // call is done, so remove active state
        })
    }
    
        2
  •  0
  •   Dhaval Pankhaniya    6 年前

    您只需禁用按钮,直到ajax完成其操作

    function select(element){
        $(element).prop('disabled', true);
        $.ajax({
            url'url',
            success:function(response){
                $(element).prop('disabled', false);
            }
        });
    }