Attach click-event to element in .select2-result

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

    This is similar in concept to netme's but the select2 event is wrong(maybe version difference), and you need to use stopPropagation to prevent item from being selected:

    http://jsfiddle.net/ZhfMg/

    $('#mySelect2').on('select2-open', function() { 
        $('.select2-results .info').on('mouseup', function(e) { 
            e.stopPropagation();
            console.log('clicked');
        }); 
    });
    

    If used with x-editable. What this does is when the x-editable goes into edit mode(shown event), that's when a select2 exists and you can wire to it's open function.

    $('#myXEditable').on('shown', function () {
        $(this).data('editable').input.$input.on('select2-open', function() { 
            $('.select2-results .info').on('mouseup', function(e) { 
                e.stopPropagation();
                console.log('clicked');
            }); 
        });
    });
    

提交回复
热议问题