keydown event in drop down list

前端 未结 12 1617
日久生厌
日久生厌 2020-12-07 00:24

I am trying to create a Drop down list, that when a user holds the SHIFT key, it will select the same index on all other drop down lists.

Currently, I am doing the f

12条回答
  •  天涯浪人
    2020-12-07 01:15

    First of all. You should pick an event to register. Don't register to keyup and keydown the same time. You give the browser a hard time, because they affect your end result. To see what I mean, just edit the plunk, add a keyup event. The end result behaves a little sloppy.

    keyup: Event fired when a key is released on the keyboard.

    keydown: Event fired when a key is pressed on the keyboard.

    keypress: Event fired when a key is pressed on the keyboard.

    They have their differences, better stick to one, I prefer for this example to use keydown, just plays better with me, you can use keyup.

    Edit: A quick note. The keyup for my example doesn't play well, because it seems, change in the selectedIndex, comes first and then the binded event. On keydown, first the event fires, does it's work and then the selectedIndex changes. To play with keyup, the code below needs some modification, that means the step is not needed, when you use keyup

    I have a plnkr demo here.

    I've tested it on IE10, Opera, Safari, Firefox and Chrome. As you might expect, webkit browsers, don't fire the keydown/keyup/keypress event when a select list has focus. Reason unknown for me, at the moment. In Firefox works great. On IE works partially. So, in order to achieve your goal, custom code to the rescue! I just binded a change event to the document, i hear for kewdown and change. If the event type is change, then the workaround comes into play. Here some code:

    $(document).ready(function(){
        $(document).on('keydown change', 'select', function(evt){
            var shiftKey = evt.shiftKey;
            var code = evt.keyCode || evt.which;
    
            //if it's a change event and i dont have any shiftKey or code
            //set the to a default value of true
            if(evt.type === 'change' && !shiftKey && !code){
              //special! only when change is fired
              shiftKey = true;
              code = true; 
            }
    
            //if shift key
            if(shiftKey && (code === 40 || code === 38 || code === true)){
    
              var target = $(evt.target);
              //if code is not true means it is not a change event, go ahead and set
              //a step value, else no step value
              var step = (code !== true) ? (code === 40) ? 1 : -1 : 0;
              var index = target[0].selectedIndex + step;
    
              //just to keep the lower and upper bound of the select list
              if(index < 0){
                index = 0;
              }else if(index >= target[0].length){
                index = target[0].length - 1;
              }
    
              //get all other select lists
              var allOtherSelects = target.closest('div').siblings('div').children('select');
              //foreach select list, set its selectedIndex
              $.each(allOtherSelects, function(i, el){
                el.selectedIndex = index;
              });
            }
          });
        });
    

提交回复
热议问题