在最初隐藏的具有多个属性的<select>
上使用Select2时,占位符不会显示。查看代码,我可以看到选择器.select2-container--default .select2-search--inline .select2-search__field
的宽度为0px(猜测因为它是隐藏的)。
$('[data-trigger="open"]').on('click', function(){
$('[data-target="open"]').toggleClass('hidden');
})
$('.select2').select2({
width: '100%'
});
.hidden {
display:none;
}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<p><a href="#" data-trigger="open">Open Select2 and watch no placeholder...</a></p>
<div class="hidden" data-target="open">
<select name="select" id="select" class="select2" multiple data-placeholder="Select one or more...">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
强制width: 100% !important
CSS规则只能部分解决问题:占位符显示但输入字段对每个项目选择都会增长太多
$('[data-trigger="open"]').on('click', function(){
$('[data-target="open"]').toggleClass('hidden');
})
$('.select2').select2({
width: '200px' // limit size to make the "growing "problem appear
});
.hidden {
display:none;
}
.select2-container--default .select2-search--inline .select2-search__field {
width: 100% !important;
}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<p><a href="#" data-trigger="open">Open Select2: placeholder ok, but field grows abnormally when inserting items...</a></p>
<div class="hidden" data-target="open">
<select name="select" id="select" class="select2" multiple data-placeholder="Select one or more...">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
请帮忙吗?
答案 0 :(得分:5)
在CSS下方添加到您的页面
.select2-selection--multiple .select2-search__field{
width:100%!important;
}
答案 1 :(得分:1)
问题是,如果默认情况下div被隐藏,则会删除占位符。
请看下面的例子。
$('[data-trigger="open"]').on('click', function() {
$('[data-target="open"]').toggleClass('hidden');
})
$('.select2').select2({
width: '100%',
placeholder: function() {
$(this).data('placeholder');
}
});
$('.select2').parent().addClass("hidden")
&#13;
.hidden {
display: none;
}
&#13;
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<p><a href="#" data-trigger="open">Open Select2 and watch no placeholder...</a></p>
<div class="" data-target="open">
<select name="select" id="select" class="select2" multiple data-placeholder="Select one or more...">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
&#13;