How to remove selected option from the option list in select2 multiselect and show selected options in the order they are selected

前端 未结 7 1310
一生所求
一生所求 2021-02-02 08:19

I have select2 multi select field in my form where I want to remove the selected option from the dropdown list after it is selected and again add it to the list if it is removed

7条回答
  •  名媛妹妹
    2021-02-02 08:43

    Remove (UnSelect) item from select2 multiple selected items

    first step: add 'multiple' css class to your select2 element to get the true element which you want

    
    
        $('select.multiple').on('select2:selecting', function (e) {
            var select = this;
            var idToRemove = '0';
            var selections = $(select).select2('data');
    
            var Values = new Array();
    
            for (var i = 0; i < selections.length; i++) {
                if (idToRemove !== selections[i].id) {
                    Values.push(selections[i].id);
                }
            }
            $(select).val(Values).trigger('change');
        });
    

提交回复
热议问题