Form does not commit because of jquery’s $("#div"). attr("disabled",true); command. What to do to resolve?

Asked

Viewed 326 times

1

$("#button").click(function(){
    $("#button").attr("disabled",true);
    $("#button").animate({opacity:0.2});

    var nome    = $("#nome").val();
    var telefone    = $("#tel").val();
    var email   = $("#email").val();
    var onde    = $("#onde").val();
    ......         
)};

In IE and Firefox works well, when I click the button it sends the data via jquery to a page and has to disable the button, but does not send the data when it is by google Chrome.

2 answers

1

Put a setTimeout 1 millisecond in the action of locking the button:

$('button').on('click',function(){
    var btn = $(this);
    setTimeout(function(){
        btn.attr('disabled',true);  
    },1);
   //...
});

This way it does not stop sending the form but still locks up fast enough to avoid more user clicks.

Exemplo

0

Why don’t you use the onsubmit form to carry out the lock?

$("#form").submit(...);
// Ou
$("#form").on('submit', (function(){
    $("#button").attr("disabled",true);
    $("#button").animate({opacity:0.2});

    var nome    = $("#nome").val();
    var telefone    = $("#tel").val();
    var email   = $("#email").val();
    var onde    = $("#onde").val();
    ......
)};

I believe this behavior is correct, but it is not coherent for all browsers...

It is true that if the button which submits the form it will not submit. I believe that using the event of onsubmit is more certain because it occurs during the submit and does not run the risk of blocking the submit.

Take a look at the jQuery documentation for the submit.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.