jquery: remove select options based on attribute array & other select box selection

ⅰ亾dé卋堺 提交于 2019-12-06 09:31:53

Proof: http://jsfiddle.net/iambriansreed/KBKEV/

var options = $('#selectbox2').html();
//store original options
$("#selectbox1").change(function() {
    var selected = this.value.split(',');
    // get selected value and make it an array
    $('#selectbox2').html(options);
    // reset the box to the original options
    $('#selectbox2 option').filter(function(){
        if(this.value.indexOf(',') == -1){
            // simple single values
            return $.inArray(this.value, selected) == -1;
        }else{  
            // check each value
            var values = this.value.split(',');
            for(i in values){
                if($.inArray(values[i], selected) > -1)
                    return false;            
            }
            return true;  
        }  
    }).remove();    
    // remove any options not in the array of selected values
});​

A working fiddle and a step by step explanation. Another quality answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!