How to stop return key from submitting form


#1

I’ve got an ajaxed form going that is set up to have the user type
something in a text field and hit a button that throws that entry into a
table on the page. User is expected to enter several items, and then
submit the form which goes to the next page.

I would like the user to be able to enter an item and hit the return key
to toss it into the table, but either Rails or the browser wants to help
me out by hooking the return key up to the main submit button.

I put in a form_observer in hopes that I could trap the return key in
there, but no joy; the submit button “sees it” first and goes on to the
next page instead of adding the item.

I did a search and all I found was the addition of some javascript to
the options for the field tag, like this:
:onkeypress => “return event.keyCode != 13 || ($(‘add’).click(),false)”

Unfortunately this doesn’t seem to work either. Also didn’t work with a
‘10’ in there (I’m on a Mac so I thought it might see it as a LF
instead).

Any help for a robust way to detect the return key before the “submit
button sees it”?

thanks,
jp