Francesco1984
Francesco1984

Reputation: 531

jQuery how to allow submit form after stopping it

I made a function where using e.preventDefault()I stopped form submitting because I need first to check the values inside. How can I do to allowing submitting after that?

Upvotes: 0

Views: 30

Answers (2)

Andrew
Andrew

Reputation: 20061

You could use e.preventDefault() on a button, rather than an input with type submit.

So change the <input type="submit"/> to a <button id="btnSubmit">Submit</button>

Then use an event handler to catch the event, and if it is successful, submit the form with $('form').submit();, for example:

$('document').on('click','#btnSubmit',function(){
   e.preventDefault()

   //do your error handling
   //if successful
   $('#formId').submit();
}

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68400

Assuming you're handling form submit event, remove e.preventDefault() so you can implement and alternative approach.

You should return false when you want to cancel submit and simply continue with normal flow you don't. Example:

$('form').submit(function() {
    if (!isFormInvalid()) {
        // cancel form submit
        return false;
    }

    // continue with function flow if form is valid
})

Upvotes: 2

Related Questions