Attach click-event to element in .select2-result

后端 未结 12 1814
猫巷女王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:46

    I think Select2's handling of the mouseup event prevents the .info element from getting a click event. Having the .info element capture the mouseup event and prevent it from propagating seems to fix that.

    function format(state, container) {
        if (!state.id) return state.text; // optgroup
        container.append(state.text);
        $('link')
            .appendTo(container)
            .mouseup(function(e) {
                e.stopPropagation();            
            })
            .click(function(e) {
                e.preventDefault();
                alert('CLICK');
            });
    }
    

    This can also be done to support a link in the selected item, but there it is the mousedown event that needs to be captured.

    jsfiddle

提交回复
热议问题