Selecting multiple from an html select element without using ctrl key

前端 未结 7 2250
灰色年华
灰色年华 2020-11-27 05:44

I\'ve come across various solutions to this issue on the net.

Basically, I find having to hold down ctrl a bit cheesy, and I want the select list to just select what

7条回答
  •  忘掉有多难
    2020-11-27 06:08

    This worked better for me than the other suggestions, because it is vanilla JS and it behaves more similarly to the original still, for example allows quick selection of many items via the shift key.

    element.onmousedown = function(event) {
        if (event.shiftKey) return;
        event.preventDefault();
        this.focus();
        var scroll = this.scrollTop;
        event.target.selected = !event.target.selected;
        this.scrollTop = scroll;
        // make sure that other listeners (like Vue) pick up on the change
        // even though we prevented the default.
        this.dispatchEvent(new Event("change"));
    }
    

提交回复
热议问题