Prevent Enter from selecting a single item in a list box
I have a multi select list box. I want to be able to select a number of items and act on the selection by pressing the Enter key. The problem I'm having is that when I press Enter the current item at the cursor becomes the only item selected. The others are deselected so my function only acts on a single item. What can I do to prevent the Enter key from selecting an item?
I am setting up the onkeypress f开发者_开发技巧unction in javascript as follows. The doListKeyPress checks for the enter key and acts on it.
select.onkeypress = function(e) { doListKeyPress(e, "myListName"); };
Add this event:
select.onkeydown = function(e) { if(e.keyCode == 13) select.blur(); };
As keydown
comes before keypress
, it will set the focus away from the listbox hence the default browser Enter
behavior is 'bypassed'.
Disclaimer: This answer does not allow selection of item by Enter. It can be done with further implementation though.
精彩评论