質問

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