Preventing a form from submitting on ENTER

Hi - I have the following problem: my app has a form that acts as a filter to a table on the page, so that the user can search any value in the table (via text box) or use select lists for more specific search. The form is being observed by an observe_form helper that runs every 1-2 seconds or so. It's all still in testing.

I noticed that when I hit return while in the textbox, the form posts and redirects me to an action unknown page. I want to remove that functionality so that hitting enter wont do anything -- any idea how to do it?

Thanks

How about removing form tags - just have the field and the observe_field … this might not be valid html - not sure about that.

Ivor

Ivor Paul wrote:

How about removing form tags - just have the field and the observe_field .... this might not be valid html - not sure about that.

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&quot;&gt;

<html xmlns="http://www.w3.org/1999/xhtml&quot; xml:lang="en"> <head>   <title>untitled</title> </head> <body>   <div>     <label for="test">Test</label><input type="text" name="test" value="" id="test" />   </div> </body> </html>

I believe if you put a textarea in your form, the browser will not submit the form if you hit the enter key. It depends on which browser you are using though.

Using and developing exclusively for firefox -- one of the benefits of building a site that'll only be run in an office intranet :slight_smile:

I'll give those a try - thanks.

lucky you :slight_smile: kudos to the staff for all using firefox

Create a form without a submit tag. Put in another control on the web page that does $('id_of_my_form').submit(); using a javascript onclick event or whatever.

Regards, Mukund

sa 125 wrote:

You can add Javascript to check for the value of the key pressed and ignore it if it is the Enter key. I had to do the exact same thing for the Backspace key not long ago, which was working as the Back icon in some instances.

I have a sample but not here. I could give it to you if you still need it tonight when I get home.

Pepe

form_for results in a POST operation. You are checking for request.xhr? in your controller to render the partial. Use remote_form_for instead to get a XHR request.

Pressing an enter key in a text field doesn't submit a form. The tags don't add that in.

Regards, Mukund

Mukund wrote:

form_for results in a POST operation. You are checking for request.xhr? in your controller to render the partial. Use remote_form_for instead to get a XHR request.

Pressing an enter key in a text field doesn't submit a form. The tags don't add that in.

Regards, Mukund

I changed the form_for to remote_form_for, and this disabled the search for the new view - Thanks. Using firebug I still see it's getting a 404 error when I hit enter in the text field. so I'm not sure what's submitting the form on enter..

Also, I'm afraid I'm not sure what you mean with the XHR part - should I just change render :partial => 'person' if request.xml_http_request? to render :partial => 'person' now that remote_form_for submits an ajax request?

Thanks for your help!