How make select2 placeholder for search input

后端 未结 4 1091
渐次进展
渐次进展 2021-01-17 14:50

How to make placeholder for select2 jQuery plugin. On StackOverflow many answers how to make placeholder there, but they about element\'s placeholder. I need to specify a pl

4条回答
  •  無奈伤痛
    2021-01-17 15:26

    You can use the event:

    select2:opening: Triggered before the dropdown is opened. This event can be prevented

    It's enough to add the placeholder attribute in this event:

    $(this).data('select2').$dropdown.find(':input.select2-search__field').attr('placeholder', 'My Placeholder')
    

    $('select').select2({
        placeholder: 'Select an option'
    }).on('select2:opening', function(e) {
        $(this).data('select2').$dropdown.find(':input.select2-search__field').attr('placeholder', 'My Placeholder')
    })
    
    
    
    
    
    

提交回复
热议问题