Attach click-event to element in .select2-result

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

    Because Select2 lib prevent any click events on popover list you can't bind events to .info directly. But you can redefine onSelect method and place there any code you want.

    See example: http://jsfiddle.net/f8q2by55/

    Update for multiple selects: http://jsfiddle.net/6jaodjzq/

    0 讨论(0)
  • 2020-11-29 02:25

    For versions of Select2 before 4.0.0 (so 3.5.x and below), you should refer to this answer about binding to onSelect.

    In newer versions of Select2, events are no longer killed within the results so you can just bind to the mouseup/mousedown events like you normally would. You should avoid binding to the click event because by default browsers will not trigger it.

    $(".select2").select2({
      templateResult: function (data) {
        if (data.id == null) {
          return data.text;
        }
        
        var $option = $("<spam></span>");
        var $preview = $("<a target='_blank'> (preview)</a>");
        $preview.prop("href", data.id);
        $preview.on('mouseup', function (evt) {
          // Select2 will remove the dropdown on `mouseup`, which will prevent any `click` events from being triggered
          // So we need to block the propagation of the `mouseup` event
          evt.stopPropagation();
        });
        
        $preview.on('click', function (evt) {
          console.log('the link was clicked');
        });
        
        $option.text(data.text);
        $option.append($preview);
        
        return $option;
      }
    });
    <link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.css" rel="stylesheet"/>
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.js"></script>
    
    <select class="select2" style="width: 200px;">
      <option value="https://google.com">Google</option>
      <option value="https://mail.google.com">GMail</option>
    </select>

    In this example we stop the propagation of the mouseup event so the browser will trigger the click event. If you are not working with actual links, but instead need to just catch a click event, you should just hook into the mouseup event.

    0 讨论(0)
  • 2020-11-29 02:26

    Just add 'open' listener and set up another 'mouseup' listener for the '' tag:

    $("#select").on('open', function() { 
        $('.select2-results i').on('mouseup', function() { 
           alert('aaa');
        }); 
    });
    

    Here is the link to my solution: http://jsfiddle.net/EW8t7/

    0 讨论(0)
  • 2020-11-29 02:28

    You need to unbind the mouseup event from the .select2-results__options element. I'm doing it in my templateResult function:

    function templateResult(location) {
    
        var $location = $([html with links]);
    
        $('.select2-results__options').unbind('mouseup');
    
        return $location;
    }
    

    If you want to keep select2's default behavior of selecting the option and closing the dropdown, you'll need manually trigger those events:

    $('.select2-results__options').unbind('mouseup');
    
    $('a', $location).on('click', function (e) {
        $('#locations_dropdown').select2('val', location.id);
        $('#locations_dropdown').select2('close');
    });
    
    return $location;
    
    0 讨论(0)
  • 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');
            }); 
        });
    });
    
    0 讨论(0)
  • 2020-11-29 02:37

    use default select2select event trigger instead of using other jquery events

    $('#mySelect2').on('select2:select', function (e) {
        var data = e.params.data;
        console.log(data);
    });
    

    for more details refer below link https://select2.org/programmatic-control/events

    0 讨论(0)
提交回复
热议问题