The submit method
The submit method allows us to work with the submit JavaScript event. It allows us to bind a handler to the submit JavaScript event or to trigger this event on an element.
Syntax
We can pass a handler function to the method, which will be triggered every time an event occurs, to which the event object can in turn be passed:
.submit(handler function);
In this case, the method will work similarly to the .on('submit', handler) construction of the on method.
We can also optionally pass to the method an object with data that will be passed to the handler function:
.submit([event data], handler function);
Or you can not pass any parameters, then the method will work similarly to the .trigger('submit') method trigger:
.submit();
The submit event occurs on an element when, for example, the user clicks on the 'submit' button on a form. This event can only be attached to elements of the form tag. The submit event occurs on an element when, for example, the user clicks on the 'submit' button on a form. This event can only be attached to elements of the form tag.
Example
In the following example, we have a simple form to which we will bind an event handler using the submit method, with a text field and a button - an input with the submit type. Let's enter text into the text field, and when the button is clicked, check this text. If we enter 'jQuery', then in the span we will show the text 'Good!', using the methods text and show, and if something else, then - 'Bad...':
<p>jQuery</p>
<form action="/">
<div>
<input type="text">
<input type="submit">
</div>
</form>
<span></span>
$('form').submit(function(event) {
event.preventDefault();
if ($('input').first().val() === 'jQuery') {
$('span').text('Good!').show();
return;
}
$('span').text('Bad...').show();
});