It's probably because your
input
is within a
form
, and when you press the ENTER button it's submitting the form after your Javascript code is executed. The reason it probably works after you've added
#
to the end of your URL is because the page doesn't navigate to itself again.
Consider making your
search
method return
false
if the ENTER key is pressed, which should suppress the submit. Otherwise, also considering binding directly to the form submit by using
$('form').on('submit', function (e) {
e.preventDefault();
return false;
});
This means you don't need to monitor for key-presses and it will ensure that the form is not actually submitted, but just the JS code is run.