Do all textboxes ( <input type="text" ) consider "PRESSING THE ENTER KEY" as submit action?

IF yes, do all browser supports this?

If no, the what's wrong with my code detecting the enter key on press?

$('#quick-search-input').bind('keyup', function(e) {
  if ( e.keyCode === 13 || $('#the-button').val() ) {
     alert("pressed");
  }
});

This code alerts on all key presses, not only on enter. Something must be wrong. I'm using jquery 1.4 BTW and don't ask why. Can't upgrade for now.

有帮助吗?

解决方案

Problem

you are checking if pressed key is eneter or textbox has value if any of condition is true than if condition will work

if ( e.keyCode === 13 || $('#the-button').val() ) {


Change it to

if ( e.keyCode === 13) {
   alert("Enter Pressed");
}

If you want value should not be empty than

if ( e.keyCode === 13 && $.trim($('#the-button').val()) !== '') {
   alert("Enter Pressed");
}

其他提示

Minor changes in your code logic,

$('#quick-search-input').bind('keyup', function(e) {
  if ( e.keyCode === 13 && $('#the-button').val()!="" ) {
     alert("pressed");
  }
});

It will better to use e.which instead of e.keyCode as per jQuery documentation

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top