开发者

Prevent Enter from selecting a single item in a list box

开发者 https://www.devze.com 2022-12-10 13:10 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消