Select2 open dropdown on focus

前端 未结 16 2198
陌清茗
陌清茗 2020-12-05 00:21

I have a form with multiple text inputs and some select2 elements. Using the keyboard to tab between fields works fine - the Select2 element behaves like a form element and

相关标签:
16条回答
  • 2020-12-05 00:36

    a bit late... but to share my code using select2 4.0.0

    $("#my_id").select2();
    $("#my_id").next(".select2").find(".select2-selection").focus(function() {
        $("#my_id").select2("open");
    });
    
    0 讨论(0)
  • 2020-12-05 00:37

    Here is an alternate solution for version 4.x of Select2. You can use listeners to catch the focus event and then open the select.

    $('#test').select2({
        // Initialisation here
    }).data('select2').listeners['*'].push(function(name, target) { 
        if(name == 'focus') {
            $(this.$element).select2("open");
        }
    });
    

    Find the working example here based the exampel created by @tonywchen

    0 讨论(0)
  • 2020-12-05 00:37

    an important thing is to keep the multiselect open all the time. The simplest way is to fire open event on 'conditions' in your code:

    <select data-placeholder="Choose a Country..." multiple class="select2-select" id="myList">
        <option value="United States">United States</option>
        <option value="United Kingdom">United Kingdom</option>
        <option value="Afghanistan">Afghanistan</option>
        <option value="Aland Islands">Aland Islands</option>
        <option value="Albania">Albania</option>
        <option value="Algeria">Algeria</option>
    </select>
    

    javascript:

    $(".select2-select").select2({closeOnSelect:false});
    $("#myList").select2("open");
    

    fiddle: http://jsfiddle.net/xpvt214o/153442/

    0 讨论(0)
  • 2020-12-05 00:39

    This worked for me using Select2 v4.0.3

    //Initialize Select2
     jQuery('.js-select').select2();
    
    // Make Select2 respect tab focus
    function select2Focus(){
        jQuery(window).keyup(function (e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            if (code == 9 && jQuery('.select2-search__field:focus').length) {
                jQuery('.js-select').select2('open');
            }
        });
    }
    
    select2Focus();
    

    Fork of Irvin Dominin's demo: http://jsfiddle.net/163cwdrw/

    0 讨论(0)
  • 2020-12-05 00:42

    For Version 3.5.4 (Aug 30, 2015 and earlier)

    The current answer is only applicable to versions 3.5.4 and before, where select2 fired blur and focus events (select2-focus & select2-blur). It attaches a one-time use handler using $.one to catch the initial focus, and then reattaches it during blur for subsequent uses.

    $('.select2').select2({})
      .one('select2-focus', OpenSelect2)
      .on("select2-blur", function (e) {
        $(this).one('select2-focus', OpenSelect2)
      })
    
    function OpenSelect2() {
      var $select2 = $(this).data('select2');
      setTimeout(function() {
        if (!$select2.opened()) { $select2.open(); }
      }, 0);  
    }
    

    I tried both of @irvin-dominin-aka-edward's answers, but also ran into both problems (having to click the dropdown twice, and that Firefox throws 'event is not defined').

    I did find a solution that seems to solve the two problems and haven't run into other issue yet. This is based on @irvin-dominin-aka-edward's answers by modifying the select2Focus function so that instead of executing the rest of the code right away, wrap it in setTimeout.

    Demo in jsFiddle & Stack Snippets

    $('.select2').select2({})
      .one('select2-focus', OpenSelect2)
      .on("select2-blur", function (e) {
        $(this).one('select2-focus', OpenSelect2)
      })
    
    function OpenSelect2() {
      var $select2 = $(this).data('select2');
      setTimeout(function() {
        if (!$select2.opened()) { $select2.open(); }
      }, 0);  
    }
    body {
      margin: 2em;
    }
    
    .form-control {
      width: 200px;  
      margin-bottom: 1em;
      padding: 5px;
      display: flex;
      flex-direction: column;
    }
    
    select {
      border: 1px solid #aaa;
      border-radius: 4px;
      height: 28px;
    }
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.4/select2.css">
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.4/select2.js"></script>
    
      
      <div class="form-control">
        <label for="foods1" >Normal</label>
        <select id="foods1" >
          <option value=""></option>
          <option value="1">Apple</option>
          <option value="2">Banana</option>
          <option value="3">Carrot</option>
          <option value="4">Donut</option>
        </select>
    </div>
    
    <div class="form-control">
      <label for="foods2" >Select2</label>
      <select id="foods2" class="select2" >
        <option value=""></option>
        <option value="1">Apple</option>
        <option value="2">Banana</option>
          <option value="3">Carrot</option>
          <option value="4">Donut</option>
        </select>
      </div>

    0 讨论(0)
  • 2020-12-05 00:42

    Something easy that would work on all select2 instances on the page.

    $(document).on('focus', '.select2', function() {
        $(this).siblings('select').select2('open');
    });
    

    UPDATE: The above code doesn't seem to work properly on IE11/Select2 4.0.3

    PS: also added filter to select only single select fields. Select with multiple attribute doesn't need it and would probably break if applied.

    var select2_open;
    // open select2 dropdown on focus
    $(document).on('focus', '.select2-selection--single', function(e) {
        select2_open = $(this).parent().parent().siblings('select');
        select2_open.select2('open');
    });
    
    // fix for ie11
    if (/rv:11.0/i.test(navigator.userAgent)) {
        $(document).on('blur', '.select2-search__field', function (e) {
            select2_open.select2('close');
        });
    }
    
    0 讨论(0)
提交回复
热议问题