如何使用jquery禁用click事件一段时间?

时间:2011-11-10 13:02:59

标签: javascript events jquery click

我有一棵左树。它的结构就像这样


    MainCategory
      Category1
        Subcategory1
        Subcategory2
      Category2
        Subcategory3
        Subcategory4
        Subcategory5
      Category3
      .
      .. etc like that      

如果用户单击任何MainCategory / Category / Subcategory,我需要禁用/防止重复/多次单击同一链接,直到结果出现。我怎么能用jquery做到这一点?

2 个答案:

答案 0 :(得分:2)

如果您使用jQuery 1.7,则可以使用off()on()功能

示例:

var foo = function () {
    // code to handle some kind of event
};

// ... now foo will be called when paragraphs are clicked ...
$("body").on("click", "p", foo);


// ... foo will no longer be called.
$("body").off("click", "p", foo); 

如果您使用旧版本,则可以unbind()bind()事件

示例:

var foo = function () {
    // code to handle some kind of event
};

// ... now foo will be called when paragraphs are clicked ...
$("body p").bind("click", foo);

// ... foo will no longer be called.
$("body p").unbind("click");

答案 1 :(得分:1)

可能只是使用一个类来检查它是否已被点击 如果单击,则不执行,并在成功和失败时删除该类。

原型 -

$('.selector').click(function(){
    if(!$(this).hasClass('someclass')){
        $(this).addClass('someclass');
        $.ajax({
          url: "test.html",
          success: function(){
            $(this).removeClass("someclass");
          },
          error: function(){
            $(this).removeClass("someclass");
          }
        });
    }
})