jQuery, block multiple clicks on the link
-
I'm trying to block multiple clicks. For buttons I got the solution, but for the links I haven't succeeded yet. It has a solution that disables the link, but does not perform the link action. I tried to use preventiveDefault, but I'm not sure he would solve my problem.
Solution for the buttons, works perfectly.
jQuery.fn.preventDoubleSubmit = function() { jQuery(this).submit(function() { console.log('preventDoubleSubmit..1'); //alert('preventDoubleSubmit..1'); if (this.beenSubmitted) { console.log('preventDoubleSubmit..2'); //alert('preventDoubleSubmit..2'); return false; } else { console.log('preventDoubleSubmit..3'); //alert('preventDoubleSubmit..3'); this.beenSubmitted = true; } }); };
jQuery('form').preventDoubleSubmit();
Solution 1 to link, nothing happens when clicking.
$("a").click(function (e) {
console.log('Cliquei no link..1');
e.preventDefault();
var target = jQuery(this);
console.log("You clicked!", target.length);
target.trigger("click");
console.log('Cliquei no link..2');
});
Solution 2 for link, disables the link but does not perform the action
$("a").click(function () {
console.log('Vou desabilitar');
$(this).fadeTo("fast", .5).removeAttr("href");
return true;
});
HTML
<a href='<s:url action="Pedido!edit"><s:param name="id" value="id"/></s:url>'><strong>Editar</strong></a>
Just one more detail, the links are out of the tag form.
-
In case of
link
you can use the following to disable the click:$(this).css('pointer-events', 'none');
In case of button can be something like:
$(this).prop("disabled", true );
After you run the event, you enable again.