views:

26

answers:

3

I have a form with an input element but no submit button. I use jQuery to attach an onsubmit handler:

$('#tickerbox-form').submit(function (ev) {
    alert('submit'); // I have other code that actually does something...
    return false;
});

I want the (anonymous function) handler to be invoked when the Enter button is pressed in the input field. This works for me on Safari, but not on IE. What am I doing wrong?

btw: IE does act on the Enter, and invokes a POST request on the URL for the specified "action" parameter of the form.

+2  A: 

Looks like IE doesn't submit forms on enter by default. Read here:

brad
Excellent info in the supplied link -- that explained the problem for me and I could devise a solution -- Thx!
Zhami
+2  A: 

Change your code to capture the Enter key instead:

$('#tickerbox-form').keypress(function(e){
  if (e.which==13){
    $(this).submit();
  }
});
Gert G
A: 

On IE it will work only if you have a single input field ie in your case only tickerbox-form

Even the definition of the command at jquery states this.

"Depending on the browser, the Enter key may only cause a form submission if the form has exactly one text field, or only when there is a submit button present. The interface should not rely on a particular behavior for this key unless the issue is forced by observing the keypress event for presses of the Enter key."

For your purpose if you have more fields use something like adding an attribute ontextchange or like:

$('#tickerbox-form').keydown(function (ev) {
    alert('submit'); // Check here if enter is pressed
    return false;
});
loxxy