开发者

jQuery submit handler for form with single input field doesn't trigger on IE

开发者 https://www.devze.com 2023-01-09 16:54 出处:网络
I have a form with an input element but no submit button. I use jQuery to attach an onsubmit handler:

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

$('#tickerbox-form').su开发者_开发百科bmit(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.


Change your code to capture the Enter key instead:

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


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


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;
});
0

精彩评论

暂无评论...
验证码 换一张
取 消