making hijax jQuery form submit

Kinda obvious now, but I was tearing my hair out for a long time over this.

If you are trying to hijax the default submission of a form, with Javascript or jQuery, you need to make sure the .onsubmit() or .submit() is referencing the form, not the submit button.

So if our original form looks like this:

<form name="myform" id="myform">
  <input type="text" value="whatever">
  <submit name="mysubmitbtn" id="mysubmitbtn">

Then this is wrong:

$('mysubmitbtn').submit(function () {
  //do stuff

And this is right:

$('myform').submit(function () {
  //do stuff

It is, of course, the form that is submitted - that's where all the data is - which is why we attach the .submit() handler to it. Trying to attach it to the submit button makes no sense in retrospect. Wish I'd figured this out twenty minutes ago....

Oh, and the idiotic part is that this is the second form on the same page. The other one worked fine. So clearly I knew what I was supposed to do when I wrote that form - all of two days ago.


No comments:

Post a Comment