santa
santa

Reputation: 12512

Submit form with button only + disable on submit w/jQuery

I need to be able to submit a form only when a button is clicked. Also I need to disable the button once the form is submitted.

I was thinking of adding an id to the button and adding an on click function. Am I on the right track? Still not sure how to disable the button after it is actually clicked.

$('#myButton').click(function() {

});

Upvotes: 0

Views: 511

Answers (2)

Russ Cam
Russ Cam

Reputation: 125488

You're on the right track. To disable the button after clicking, you just need to set the disabled attribute. Something like

$('#myButton').click(function() {
    // get a reference to the element on which the handler is bound
    // wrap it in a jQuery object so we can call jQuery methods on it.
    $(this)
    // set the disabled attribute. You can use true or the string 'disabled'
    .attr('disabled', true);
});

Bear in mind that a form can also be submitted by pressing the Enter key when an element inside the form has focus.

Upvotes: 2

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114347

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

Upvotes: 1

Related Questions