防止用jQuery双击链接

时间:2021-07-30 03:48:19

What's the best way to prevent a double-click on a link with jQuery?

防止双击jQuery链接的最佳方法是什么?

I have a link that triggers an ajax call and when that ajax call returns it shows a message.

我有一个触发ajax调用的链接,当ajax调用返回时它会显示一条消息。

The problem is if I double-click, or click it twice before the ajax call returns, I wind up with two messages on the page when I really want just one.

问题是如果我双击,或者在ajax调用返回之前单击它两次,当我真的只需要一个时,我最终会在页面上显示两条消息。

I need like a disabled attribute on a button. But that doesn't work on links.

我需要像按钮上的禁用属性。但这不适用于链接。

$('a').on('click', function(e){
    e.preventDefault();

    //do ajax call
});

9 个解决方案

#1


7  

You can use data- attributes, something like this:

您可以使用数据属性,如下所示:

$('a').on('click', function () {
   var $this = $(this);
   var alreadyClicked = $this.data('clicked');
   if (alreadyClicked) {
      return false;
   }
   $this.data('clicked', true);
   $.ajax({
      //some options
      success: function (data) { //or complete
         //stuff
         $this.data('clicked', false);
      }
   })
});

#2


4  

I came with next simple jquery plugin:

我带来了下一个简单的jquery插件:

(function($) {
  $.fn.oneclick = function() {
    $(this).one('click', function() {
      $(this).click(function() { return false; });
    });
  };
  // auto discover one-click elements
  $(function() { $('[data-oneclick]').oneclick(); });
}(jQuery || Zepto));

// Then apply to selected elements
$('a.oneclick').oneclick();

Or just add custom data atribute in html:

或者只是在html中添加自定义数据属性:

<a data-oneclick href="/">One click</a>

#3


1  

You need async:false

你需要async:false

By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false.

默认情况下,所有请求都是异步发送的(默认情况下设置为true)。如果需要同步请求,请将此选项设置为false。

$.ajax({
      async: false,
      success: function (data) { 

         //your message here
      }
   })

#4


1  

you can use a dummy class for this.

你可以使用一个虚拟类。

$('a#anchorID').bind('click',function(){
           if($(this).hasClass('alreadyClicked')){
              return false;
           }else{
                $(this).addClass('alreadyClicked);
                $/ajax({
                    success: function(){$('a#anchorID').removeClass('alreadyClicked');},
                    error: function(){$('a#anchorID').removeClass('alreadyClicked');}
                });
           }});

#5


0  

Check this example. You can disable the button via CSS attribute after the first click (and remove this attribute after an ajax request or with a setTimeout) or use the jQuery.one() function to remove the trigger after the first click (without disabling the button)

检查此示例。您可以在第一次单击后通过CSS属性禁用该按钮(并在ajax请求或使用setTimeout后删除此属性)或使用jQuery.one()函数在第一次单击后删除触发器(不禁用按钮)

var normal_button = $('#normal'),
  one_button = $('#one'),
  disabled_button = $('#disabled'),
  result = $('#result');

normal_button.on('click', function () {
  result.removeClass('hide').append(normal_button.html()+'<br/>');
});

one_button.one('click', function () {
  result.removeClass('hide').append(one_button.html()+'<br/>');
});

disabled_button.on('click', function () {
  disabled_button.attr('disabled', true);
  setTimeout(function () {
    result.removeClass('hide').append(disabled_button.html()+'<br/>');
  }, 2000);
});

#6


0  

Although there are some good solutions offered, the method I ended up using was to just use a <button class="link"> that I can set the disabled attribute on.

虽然提供了一些很好的解决方案,但我最终使用的方法是使用

Sometimes simplest solution is best.

有时最简单的解决方案是最好的

#7


-1  

You can disable click event on that link second time by using Jquery

您可以使用Jquery第二次在该链接上禁用click事件

$(this).unbind('click');

See this jsfiddle for reference Demo

请参阅此jsfiddle以获取参考演示

#8


-1  

You can disable your links (for instance, href="#" ), and use a click event instead, binded to the link using the jQuery one() function.

您可以禁用链接(例如,href =“#”),并使用click事件代替,使用jQuery one()函数绑定到链接。

#9


-1  

Bind all the links with class "button" and try this:

用类“按钮”绑定所有链接并尝试:

$("a.button").click(function() { $(this).attr("disabled", "disabled"); });
$(document).click(function(evt) {
     if ($(evt.target).is("a[disabled]"))
          return false;
});

#1


7  

You can use data- attributes, something like this:

您可以使用数据属性,如下所示:

$('a').on('click', function () {
   var $this = $(this);
   var alreadyClicked = $this.data('clicked');
   if (alreadyClicked) {
      return false;
   }
   $this.data('clicked', true);
   $.ajax({
      //some options
      success: function (data) { //or complete
         //stuff
         $this.data('clicked', false);
      }
   })
});

#2


4  

I came with next simple jquery plugin:

我带来了下一个简单的jquery插件:

(function($) {
  $.fn.oneclick = function() {
    $(this).one('click', function() {
      $(this).click(function() { return false; });
    });
  };
  // auto discover one-click elements
  $(function() { $('[data-oneclick]').oneclick(); });
}(jQuery || Zepto));

// Then apply to selected elements
$('a.oneclick').oneclick();

Or just add custom data atribute in html:

或者只是在html中添加自定义数据属性:

<a data-oneclick href="/">One click</a>

#3


1  

You need async:false

你需要async:false

By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false.

默认情况下,所有请求都是异步发送的(默认情况下设置为true)。如果需要同步请求,请将此选项设置为false。

$.ajax({
      async: false,
      success: function (data) { 

         //your message here
      }
   })

#4


1  

you can use a dummy class for this.

你可以使用一个虚拟类。

$('a#anchorID').bind('click',function(){
           if($(this).hasClass('alreadyClicked')){
              return false;
           }else{
                $(this).addClass('alreadyClicked);
                $/ajax({
                    success: function(){$('a#anchorID').removeClass('alreadyClicked');},
                    error: function(){$('a#anchorID').removeClass('alreadyClicked');}
                });
           }});

#5


0  

Check this example. You can disable the button via CSS attribute after the first click (and remove this attribute after an ajax request or with a setTimeout) or use the jQuery.one() function to remove the trigger after the first click (without disabling the button)

检查此示例。您可以在第一次单击后通过CSS属性禁用该按钮(并在ajax请求或使用setTimeout后删除此属性)或使用jQuery.one()函数在第一次单击后删除触发器(不禁用按钮)

var normal_button = $('#normal'),
  one_button = $('#one'),
  disabled_button = $('#disabled'),
  result = $('#result');

normal_button.on('click', function () {
  result.removeClass('hide').append(normal_button.html()+'<br/>');
});

one_button.one('click', function () {
  result.removeClass('hide').append(one_button.html()+'<br/>');
});

disabled_button.on('click', function () {
  disabled_button.attr('disabled', true);
  setTimeout(function () {
    result.removeClass('hide').append(disabled_button.html()+'<br/>');
  }, 2000);
});

#6


0  

Although there are some good solutions offered, the method I ended up using was to just use a <button class="link"> that I can set the disabled attribute on.

虽然提供了一些很好的解决方案,但我最终使用的方法是使用

Sometimes simplest solution is best.

有时最简单的解决方案是最好的

#7


-1  

You can disable click event on that link second time by using Jquery

您可以使用Jquery第二次在该链接上禁用click事件

$(this).unbind('click');

See this jsfiddle for reference Demo

请参阅此jsfiddle以获取参考演示

#8


-1  

You can disable your links (for instance, href="#" ), and use a click event instead, binded to the link using the jQuery one() function.

您可以禁用链接(例如,href =“#”),并使用click事件代替,使用jQuery one()函数绑定到链接。

#9


-1  

Bind all the links with class "button" and try this:

用类“按钮”绑定所有链接并尝试:

$("a.button").click(function() { $(this).attr("disabled", "disabled"); });
$(document).click(function(evt) {
     if ($(evt.target).is("a[disabled]"))
          return false;
});