0

If i submit form in such way:

HTML

<form id="form" name="form" action="test_url">
  <input type="text" name="test" value="">
  <input type="button" onclick="this.form.submit" value="submit">
</form>

jQuery

$('#form').submit(function() {
  alert('ok');
  return false;
});

Can I prevent submission with jQuery?

2 Answers 2

2
$('#form').submit(function(e) {
  e.preventDefault();
  alert('ok');
});

DEMO

passing event argument (here, e) to submit callback you can stop form submission using .preventDefault() method.

From jQuery doc about .preventDefault():

If this method is called, the default action of the event will not be triggered.

Note

Change

<input type="button" onclick="this.form.submit" value="submit">

to

<input type="submit" value="submit">
Sign up to request clarification or add additional context in comments.

Comments

1

yes, just passing the event along with the handler and using preventDefault() method

$('#form').submit(function(evt) {
   evt.preventDefault();
   alert('ok');
});

Reference: http://api.jquery.com/event.preventDefault/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.