How can I make an HTML multiple select act like the control button is held down always

前端 未结 4 1578
慢半拍i
慢半拍i 2020-12-17 15:47

I have a web application I\'m working on that requires a HTML multiple select element operates like the control key is held down at all times. I.e. click on an option will

4条回答
  •  太阳男子
    2020-12-17 16:12

    Try this out. You can store the option values in an object and use the click action to update the object then apply the changes to the select.

    Demo

    http://jsfiddle.net/iambriansreed/BSdxE/

    HTML

    JavaScript

    $('.select-toggle').each(function(){    
        var select = $(this), values = {};    
        $('option',select).each(function(i, option){
            values[option.value] = option.selected;        
        }).click(function(event){        
            values[this.value] = !values[this.value];
            $('option',select).each(function(i, option){            
                option.selected = values[option.value];        
            });    
        });
    });​
    

提交回复
热议问题