1
$('#search_form').submit(function(e) { //e.preventDefault(); return false; }) 

This works just fine to prevent form submission when pressing Enter.

However even I have this I want to submit the form with jquery if certain circumstances are true.

edit:

$('#search_form').submit(function(e) { return !!e.submit; }); function ... if (e.keyCode == 13) { if (blablabla) { ... // do something } else { $('#search_form').submit({submit:true}); //doesn't work console.log('submitted'); //this does successfully get fired } } 

If I press enter the form doesn't get submitted, but the log in in the console happens!

2
  • I think you'll need to post more code to troubleshoot this. As is, the syntax looks correct, so it probably isn't this part of the code that's causing the problem. Commented Feb 22, 2011 at 17:38
  • If you could re-create this on jsfiddle.net, I could help you out a lot better. Commented Nov 23, 2011 at 1:44

2 Answers 2

7
$('#search_form').submit(function(e) { if($('.errors').is(':visible')){ e.preventDefault(); // do something else here// some errors are visible :) return false; }else{ } }) 
Sign up to request clarification or add additional context in comments.

2 Comments

this doesn't work for me because I have two different functions! Like in my code above. And I cannot combine them.
u can trigger this function on a button click or somin or just simply wait for the submit button to be clicked
5

By checking the type of e.originalEvent we can determine if human (type object) or script (type undefined) submitted the form:

$('#search_form').submit(function(e) { if (typeof e.originalEvent !== 'undefined') { e.preventDefault(); } }) 

Invoke a jquery trigger to submit the form:

$('#search_form').trigger('submit'); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.