Fraz Sundal
Fraz Sundal

Reputation: 10448

Disable button on submit click in jquery

On submit button i want to disable the button so user does not click more than once. im using jquery for that but its not working, my code is

setTimeout($('#btn').attr("disabled", true), 1);
return true;  

button get disabled but my controller does not call. what am i doing wrong?

Upvotes: 2

Views: 8071

Answers (5)

user14049870
user14049870

Reputation: 1

This should work.

$("#loginButton").click(function (e) {
        var btn = $(this).clone();
        btn.prop("disabled", true)
        $(this).hide();
        $(this).after(btn);
        return true;
    });

Upvotes: 0

Sandeep Bollu
Sandeep Bollu

Reputation: 11

setTimeout($('#btn').attr('disabled', 'disabled),1);
$("#btn").removeAttr('disabled', disabled')

Upvotes: 1

sharat87
sharat87

Reputation: 7526

I don't see much reason to use timeout, I'd just go with

$('#btn').attr('disabled', 'disabled');
return true;

or more precisely,

$('#btn').click( function (e) {
    $(this).attr('disabled', 'disabled');
});

is all you need.

And being a bit wicked and esoteric :P

return !!$('#btn').attr('disabled', 'disabled');

and that, was just for fun. Don't do it in your code! :)

Edit: with a recent version of jQuery, you can make it

$('#btn').attr('disabled', true);

Upvotes: 6

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

setTimeout(function() {
    $('#btn').attr('disabled', 'disabled');
}, 1);

Upvotes: 8

Vaibhav Gupta
Vaibhav Gupta

Reputation: 1612

try this:

setTimeout($('#btn').attr('disabled', 'disabled'), 1);
return true;

Upvotes: -1

Related Questions