Skip to content Skip to sidebar Skip to footer

How Do You Determine Which Submit Was Used To Submit A Form With Jquery?

Keep in mind that the user does NOT have to click the submit nput. They could tab over to it and push enter. So considering all ways a form can be submitted, how can you determine

Solution 1:

Any action that triggers a submit button — be it an actual mouse click or some keyboard action — still triggers a "click" event and still causes the input to be included as a form parameter.

That is, if you see a form parameter with your input field's name and value, you know that that submit button was clicked.

edit — if the form submit happens because you hit "Enter" in a text field, the browser picks the first submit button (I think; that seems to be what Firefox does at least). (Wait; scratch that; Firefox seems to find the next "submit" input after the element that had focus when "Enter" was pressed ...)

Thus:

<form action='whatever' method='post'>
  <inputtype='text' name='text'>
  <input name='submit1' value='submit1'type='submit'>
  <input name='submit2' value='submit2'type='submit'>
</form>

Hitting "Enter" in the text field would result in "submit1=submit1" being a form parameter, as would hitting "Enter" when "submit1" had focus. Tabbing to "submit2" and hitting "Enter" would result in "submit2=submit2" being among the parameters.

In either case, only one of the "submit" inputs shows up in the parameter list.

Solution 2:

Instead of using different names, use different values, e.g:

<inputtype="submit" name="submit" value="Submit A">
<inputtype="submit" name="submit" value="Submit B">

You can also use buttons instead of inputs if you'd like the labels to be different from the values.

Post a Comment for "How Do You Determine Which Submit Was Used To Submit A Form With Jquery?"