Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

form submit not working correct with jquery

i have ajax validation function and when ajax validation is true then i use:

$("#test_form").submit();

it is work fine but any time i can click Enter and submit a form when form is not validated (skip validation process and pass wrong data). i try put

$("#test_form").submit(function(){event.preventDefault();});

then still can submit with enter when data is wrong or

$("#sample_form").submit(function(){false});

then i cant submit any time.

How to submit only when ajax is true?

like image 735
Klapsius Avatar asked Mar 05 '26 05:03

Klapsius


1 Answers

The first part of your answer is said in the comment section :


Use $("#test_form").submit(function(event){event.preventDefault();}); notice function(event)

– Satpal


Then you'll notice an other problem, your form will never submit after the AJAX call. That is because you use $("#test_form").submit(); to submit, which is the jQuery triggering the event. When jQuery trigger the event, it will always be prevented by the preventDefault.

What you need to do is to use the native JavaScript event :

$("#test_form")[0].submit();

When you are using the native handler, the event you have been added with jQuery will not trigger. It will instead directly send the form.

like image 109
Karl-André Gagnon Avatar answered Mar 06 '26 19:03

Karl-André Gagnon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!