tags:

views:

94

answers:

2

Hi,

I'm stuck on the above problem. I have a simple form as follows, with a text input, a select list and a submit button. When focus is on the text input and I hit Enter, the form submits. If focus is on the select list the submit does not fire. I want the form to submit regardless of which field has focus.

<html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id='text1'/>
        <select>
            <option value="volvo">Volvo</option>
            <option value="saab">Saab</option>
            <option value="mercedes">Mercedes</option>
            <option value="audi">Audi</option>
        </select>
        <input type='submit' onclick='alert("you submitted the form")'/>
    </div>
    </form>
</body>
</html>
A: 

firefox does this by default, for ie you need some type of javascript function cause IE binds the submit on enter only to inputs.

Tim
Using Firefox 3.6.8 and IE8 and it doesn't work
SWebbs
+2  A: 

It's normal browser behaviour, to select an option with enter.

What you can do is attach a listener to the keyDown event of the select element and submit the form when the user pressed enter. For example. with jQuery:

$('#your-select').keydown(function(e){  
  if (e.keyCode == 13) {
      $('#your-form').submit();
    } });

You should consider that it breaks the standards and would cause a problem for users navigating with keyboard.

Karman Kertesz
+1 for last comment. It's standard browser behaviour, leave it alone. Keyboard users need Enter to select the current option when using up/down arrows in a select box.
bobince
yep, that works but totally with you re using Enter to select a value. So...how do I test for whether the select is expanded (leave alone-standard behaviour) and closed up?
SWebbs
Convinced my user rep that it was a bad idea
SWebbs