select2 - hiding the search box

前端 未结 18 1670
太阳男子
太阳男子 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:31

    If you have multi attribute in your select, this dirty hack works:

    var multipleSelect = $('select[name="list_type"]');
    multipleSelect.select2();
    multipleSelect.parent().find('.select2-search--inline').remove();
    multipleSelect.on('change', function(){
        multipleSelect.parent().find('.select2-search--inline').remove();
    });
    

    see docs here https://select2.org/searching#limiting-display-of-the-search-box-to-large-result-sets

    0 讨论(0)
  • 2021-01-29 19:33
    //Disable a search on particular selector
    $(".my_selector").select2({
        placeholder: "ÁREAS(todas)",
        tags: true,
        width:'100%',
        containerCssClass: "area_disable_search_input" // I add new class 
    });
    
    //readonly prop to selector class
    $(".area_disable_search_input input").prop("readonly", true);
    
    0 讨论(0)
  • 2021-01-29 19:34

    I like to do this dynamically depending on the number of options in the select; to hide the search for selects with 10 or fewer results, I do:

    $fewResults = $("select>option:nth-child(11)").closest("select");
    $fewResults.select2();
    $('select').not($fewResults).select2({ minimumResultsForSearch : -1 });
    
    0 讨论(0)
  • 2021-01-29 19:35

    Removing the inputs with jQuery works for me:

    $(".select2-search, .select2-focusser").remove();
    
    0 讨论(0)
  • 2021-01-29 19:35

    You can try this

    $('#select_id').select2({ minimumResultsForSearch: -1 });
    

    it closes the search results box and then set control unvisible

    You can check here in select2 document select2 documents

    0 讨论(0)
  • 2021-01-29 19:35

    I edited the select2.min.js file to set the select-2__search input field that's generated to readonly="true".

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