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 su开发者_开发百科bmitted.
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() {
});
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.
$('#myButton').click(function() {
$(this).attr('disabled','disabled')
});
精彩评论