1
I am trying to block multiple clicks. For buttons I got the solution, but for links I have not yet succeeded. Has a solution that disables the link, but does not execute the link action. I tried to use preventDefault, but I’m not sure it would solve my problem.
Button solution, 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 to 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 form tag.
In solution 2 you could take the href attribute of the element and use localtion.href to open the link after removing the attribute.
– Matheus
Matheus, could you give me an example of what the code would look like? I think it would be a good solution. Thanks.
– Developer
Would that be the command to open the link ? window.Location = $(this). attr('href');
– Developer
When you say "multiple clicks"
dblclick
, or is the usual fast double click?– Sergio
Impatient users click the first time, after a few seconds with no response, click again. Then it would be block from the second click.
– Developer