select2 - hiding the search box

前端 未结 18 1714
太阳男子
太阳男子 2021-01-29 19:04

For my more significant selects, the search box in Select2 is wonderful. However, in one instance, I have a simple selection of 4 hard-coded choices. In this case, the search bo

18条回答
  •  日久生厌
    2021-01-29 19:21

    If you want to hide on initial opening and you are populating the dropdown via ajax call, add the following to the ajax block in your select2 declaration:

    beforeSend: function () 
      {
        $('.select2-search--dropdown').addClass('hidden');
      }
    

    To then show it again (and focus) after your ajax request is successful:

      success: function() {
          $('.select2-search--dropdown').removeClass('select2-search--hide'); // show search bar then focus
          $('.select2-search__field')[0].focus();
      }
    

提交回复
热议问题