Attach click-event to element in .select2-result

后端 未结 12 1835
猫巷女王i
猫巷女王i 2020-11-29 02:06

I\'m looking for a way to attach a click-event to a select2-result-item. I\'ve gone ahead and formatted both result and selection via

function format(state)          


        
12条回答
  •  爱一瞬间的悲伤
    2020-11-29 02:43

    the thing seems to be that the tgas will be deleted right after the box is closed. Therefore the click event cannot be fired.

    I've changed a few things + the event that will be listened on. It's now a mousedown...

    function format(state) {
        if (!state.id) return state.text; // optgroup
        return state.text + " link";
    }
    
    $("select").select2({
        formatResult: format,
        formatSelection: format,
        escapeMarkup: function(m) { return m; }
    }).on('select2-open',function(){
        console.log($('.info'))
        $('.info').on('mousedown', function(event){
            console.log("click")
            $('#log').text( "clicked" );
            event.preventDefault();
        });
    });
    
    
    
    
    
    

提交回复
热议问题