开发者

prevent the execution of function from double click

when on button click this function is called开发者_运维百科

save_edit: function() {

    this.do_save();

},

I am trying to disable a button once it is clicked to prevent double clicks. How can I do it within this function?


$("#el").click(function() {
    $(this).prop('disabled',true);
    // do more stuff
    $(this).prop('disabled',false); // if desired 
});

Or, if you have several buttons:

$(":button").click(function() {
    $(this).prop('disabled',true);
    // $(this) is a jQuery object of the button that was just clicked
    str_id = $(this).attr('id'); // if you really need the ID itself
    // do more stuff
});


$("#element").dblclick(function(){
   return false;
});

$("#element").click(function(){
  //do stuff
});


If you do not want to disable the button, you can stop the method from calling the function for a set peroid of time.

save_edit: function() {
    if(this.timer)return;
    this.do_save();
    this.timer = window.setTimeout( function(){}, 300 );  //number of ms before you want another submit to happen
},


$("#btn").one(function(){
  //do stuff once and remove handler
});

$("#btn").dblclick(function(){
   return false;
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜