Question

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.

Était-ce utile?

La solution

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

Autres conseils

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top