Is it possible to handle up/down key in HTML input

2019-07-07 20:44发布

问题:

I am building a autocomplete feature for input text field. Currently I could use mouse click to choose the selection, I'd like to add keyboard control to allow autocomplete control. I monitored the keycode in onkeyup event, it appears for up/down key, the key codes are both 0.

I am wondering whether there is any better way to do so.

Thanks.

回答1:

Keycodes 37 - 40 should do it for you. They map as 37=left, 38=up, 39=right, 40=down.



回答2:

Look at the example at http://www.w3schools.com/jsref/jsref_onkeydown.asp. It works for me in both FF3.5 and IE8.



回答3:

There's usually not a justifiable reason to re-invent the wheel. That said, I would recommend using jQuery with the AutoComplete plugin.