Auto-complete on input (but NOT in auto-select)

I have a simple input field

and after entering I want to send it (the form in which it is located). I use jQuery, but not any autocompletion extension, so this is just the default browser:

$('#searchfield') .keypress(function(event) { var keycode; if (window.event) keycode = window.event.keyCode; else if (event) keycode = event.which; else return true; if (keycode == 13) $('#searchForm').submit(); }); 

The problem with this solution: When someone (in FF3) starts typing (say, "stackov"), then the down arrow goes for the proposed choice and gets there, the form submits, although it looks for "stackov" and not for the selected "stackoverflow "

As an alternative, I tried

 $('#searchfield') .change( function() { $('#singleSearch').trigger('click'); }) 

This led me to the correct set of "stackoverflow", but now any departure from the field (as in blur (), after pressing some letters, for example) unwittingly sent it.

Any (perfectly tested;) advice on this?

+4
source share
1 answer

Hmm, all I can say is a little delay allowing me to "digest" autocomplete before sending fixes the problem:

 if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) { setTimeout( function() { $('#searchForm').submit(); } , 30 ); }; 
+1
source

Source: https://habr.com/ru/post/1347123/


All Articles