3

I have a little problem. I have already looked at many threads like:
- jquery submit function not working
- jQuery submit function not working properly
- JQuery Submit Form From Inside Submit Function

But I have the problem that my submit doesn't work. I don't see why. I would be happy if someone could explain to me why the submit button doesn't work. I don't see a log in the console submit.

My goal is:
1. click on a button which is not a submit button
2. do some stuff then
3. then start submit & do something there

<!DOCTYPE html> <html> <head>	<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> </script> </head> <body>	<form method="post" action="submit.html" id="test">	<input type="button" id="bttn" value="Submit"></input>	</form> </body> </html> <script language="javascript" type="text/javascript"> $(function() {	$( 'input#bttn' ).on( 'click', function() { // do something	console.log('click'); // start submit	$( 'form#test' ).submit( function() {	console.log('submit');	return true;	});	}); }); </script>

4
  • 5
    Your method is wrong. method="post" action="submit.html" Commented Oct 13, 2016 at 16:14
  • 1
    Good catch @JackNicholson! Commented Oct 13, 2016 at 16:16
  • 2
    You're using the wrong form of jquery's submit. What you're doing is binding an event handler, which will run when the submit event fires. If you want to manually trigger submit, then you have to use this instead: $('form#test').submit() Commented Oct 13, 2016 at 16:18
  • Then in my case I dont see the advantage of that, i just could do all before i use the submit trigger without the handler. Commented Oct 13, 2016 at 18:06

1 Answer 1

1

First mistake (no method and duplicate action): <form action="post" action="submit.html" id="test"> should be <form method="post" action="submit.html" id="test">.

Second mistake:

$( 'form#test' ).submit( function() { // handler for submit event console.log('submit'); return true; }); 

should just be

$( 'form#test' ).submit(); // triggers submit event 

And then you can add the handler for the submit event, which would be called after submit triggers:

$( 'form#test' ).submit( function() { console.log('submit'); }); 
Sign up to request clarification or add additional context in comments.

2 Comments

The first mistake was copy paste I think. So because the second mistake: - so if i use the submit as a handler, then i need to trigger it manually?
But then I dont see the advantage of that? Because I could do all the things that i want do, before i just trigger the submit without the handler...?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.