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>
Community
  • 1
  • 1
tokyodrift
  • 51
  • 9

1 Answers1

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');
});
Rax Weber
  • 3,730
  • 19
  • 30
  • 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? – tokyodrift Oct 13 '16 at 17:57
  • 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...? – tokyodrift Oct 13 '16 at 17:58