jQuery允许单击但不允许双击 [英] jQuery Allow a single click but disallow a double click

查看:67
本文介绍了jQuery允许单击但不允许双击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我原本想要在单击事件中进行提交:

I originally wanted a submit to take place on a single click event:

    $("#booking_Form #submit_Booking").bind("click", function(event){.....

然后我发现(很明显,双击导致重复提交。

I then found (obviously) that a double click led to a duplicate submission.

所以我尝试使用以下方法捕获和抑制:

So I tried capturing and suppressing this with:

    $("#booking_Form #submit_Booking").bind("dblclick", function(event){
          return false;
    });

但单击事件仍然会被触发两次。

But the single click event still fired twice.

我是纠正它认为如果必须双击不提交两次,我必须将单击事件更改为双击事件。

Am I correct it thinking that if it is imperative that a double click does not submit twice that I must change the single click event to a double click event.

或者是否有一些全局关闭双击的方法。

Or is there some global way to switch off double clicks.

推荐答案

你应该使用 .one()。那样所有后续点击都不会做任何事情

You should use .one(). That way all subsequent clicks will do nothing

$("#booking_Form #submit_Booking").one("click", function(event) {
    //do something
});

链接: http://api.jquery.com/one/

编辑:
如果你想使用 .one(),做这样的事情怎么样......

If you want to use .one(), how about doing something like this...

JAVASCRIPT:

JAVASCRIPT:

$(document).ready(function(){
     $("#b1").one("click", function(e){
        ajaxFunction();
     });   

    function ajaxFunction(){
         $("#b1").one("click", function(e){
             ajaxFunction()
         });

        $.ajax({
            type: "POST",
            url: "http://fiddle.jshell.net/", //use actual URL
            success: function(data){
               //do something    
            }
        });           
    }
});​

DEMO:
http://jsfiddle.net/BKqm9/13/

这篇关于jQuery允许单击但不允许双击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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