gpt4 book ai didi

jquery - 如果最初隐藏,Select2 不会显示占位符

转载 作者:行者123 更新时间:2023-12-01 00:35:09 25 4
gpt4 key购买 nike

在最初隐藏的 <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>

请问有什么帮助吗?

最佳答案

将以下 css 添加到您的页面

.select2-selection--multiple .select2-search__field{
width:100%!important;
}

关于jquery - 如果最初隐藏,Select2 不会显示占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50696987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com