Javascript 启用select2多选搜索框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36188356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable select2 multi-select search box
提问by John Brink
I need to be able to add a search box to my multi-select fields using select2.
我需要能够使用 select2 在我的多选字段中添加一个搜索框。
For whatever reason, while search boxes appear as expected in single-select fields, the same select2() call on a multi-select field does not add a search box.
无论出于何种原因,虽然搜索框在单选字段中按预期显示,但对多选字段的相同 select2() 调用不会添加搜索框。
var data = []; // Programatically-generated options array with > 5 options
var placeholder = "select";
$(".mySelect").select2({
data: data,
placeholder: placeholder,
allowClear: false,
minimumResultsForSearch: 5});
Does select2 not support search boxes with multi-selects? Does anyone have a good similarly-functioning alternative?
select2 不支持多选搜索框吗?有没有人有一个很好的类似功能的替代品?
采纳答案by Jay Rizzi
The answer is that the select2 input element becomesthe search box for multiple selects without back end data
答案是select2输入元素变成没有后端数据的多选搜索框
if you start typing , your results will start filtering the options
如果您开始输入,您的结果将开始过滤选项
if you have it set to load remote ajax data, it actually does retain a search box, but for multiple selects without a data source, the input is the search bar, which is fairly intuitive
如果设置为加载远程ajax数据,其实还是保留了一个搜索框,但是对于没有数据源的多选,输入的是搜索栏,比较直观
回答by Sadee
select2 v4.0.3
选择2 v4.0.3
<select class="smartsearch_keyword" name="keyword[]" id="keyword" style="width:100%;"></select>
$(".smartsearch_keyword").select2({
multiple: true,
...
});
In addition: to set multiple default selections
另外:设置多个默认选择
回答by Waiyl Karim
If none of the answers above work for you (new comers), consider wrapping everything between $(document).ready(function() { // your select2 declaration here... });
Sometimes it's a common issue!
如果以上答案都不适合您(新人),请考虑将所有内容包装在$(document).ready(function() { // your select2 declaration here... });
有时这是一个常见问题!