Jquery – Select optgroup in select

后端 未结 3 1228
忘了有多久
忘了有多久 2021-01-12 01:00

I have a select with 2 optgroups. Is there a way to call an function only if an option from the first optgroup is selected and call another function if an option from the se

3条回答
  •  花落未央
    2021-01-12 02:00

    When you click on option you get the Id name of option group.

        var obj = {};
    $('select[name=queue]').on('change', function () {
        obj = {};
        var options = $('option:selected', this); //the selected options
        $.each(options, function (index, option) {
            var optgroupIndex = $(option).closest('optgroup').index() //get the index
            var optgroupId = $(option).parent()[0].id //get id
            if (obj.hasOwnProperty(optgroupId)) {
                obj[optgroupId].push($(option).val());
            } else {
                obj[optgroupId] = [$(option).val()];
            }
        });
        var textRows = [];
        $.each(obj, function(optgroupId, values){
            textRows.push(optgroupId +": " + values.join(', '));
        });
        $('#demo').html(textRows.join("
    ")); });
     /*Additional*/
    select::-webkit-scrollbar {display:none;}
    select::-moz-scrollbar {display:none;}
    select::-o-scrollbar {display:none;}
    select::-google-ms-scrollbar {display:none;}
    select::-khtml-scrollbar {display:none;}
    select{height:150px;}
    
    
    
    
    

提交回复
热议问题