我正在使用 Twitter 引导程序和出色的 Select2 插件。
这些工作得很好,我意识到你需要{width: 'resolve'}
在启动 Select2 时进行设置,否则它看起来会一团糟!。
但是我的一个选择有问题,如下图所示,Referee Type
选择的宽度不正确。
这是由于该字段最初是隐藏的,并且只有在“组”字段中选择了“裁判”时才可见。
那么,我该如何解决这个问题?
我正在使用 Twitter 引导程序和出色的 Select2 插件。
这些工作得很好,我意识到你需要{width: 'resolve'}
在启动 Select2 时进行设置,否则它看起来会一团糟!。
但是我的一个选择有问题,如下图所示,Referee Type
选择的宽度不正确。
这是由于该字段最初是隐藏的,并且只有在“组”字段中选择了“裁判”时才可见。
那么,我该如何解决这个问题?
Select 2
只要您在select
标签中设置它,它就足够聪明,可以重新计算它的大小。像这样:
<select style="width: 100%"></select>
类上的 CSS 格式不起作用!
在“响应式设计 - 百分比宽度”中阅读更多内容
<select>
您可以像这样设置元素中的宽度:
<select style="width:260px">
直到找到所需的宽度。其实select2插件官方网站上的例子就是这样完成的。
但是,如果您打算远离内联样式,则始终可以简单地覆盖 select2 的 CSS 并以生成的容器为目标。
.select2-container {
width: 260px;
}
我用简单的 CSS 解决了我的类似问题:
.select2-container {
width: 100% !important;
}
不是select2()
在页面加载时调用Referee Type,而是select2
在第一次显示Referee Type后调用。这样,选择将是可见的(具有适当的宽度),并且该'width': 'resolve'
选项应该可以工作。如果您提供一个小的 jsfiddle 示例,则更容易指出这一点。
select
设置宽度为 100% 的元素的样式属性
<select class="js-example-responsive" style="width: 100%"></select>
.select2-container {
width: 100% !important;
}
当您取消隐藏附加了 Select2 的控件时执行此操作:
//refresh select2 controls (to correct offscreen size bad calculation)
$("SELECT.select2-offscreen").select2();
您可以将单击事件绑定到打开隐藏元素的元素并在那里设置 select2 宽度。为我工作,这很简单。我使用了单击事件,因为仅使用“文档准备就绪”进行操作是行不通的。
$('#click-me').on('click', function(event) {
$('.select2').select2({width: '100%'});
}
我首选的解决方案是停止select2
分配这些width
并让它们自动进行。在 中评论以下内容select2.full.js
:
对于容器:
Select2.prototype._placeContainer = function ($container) {
$container.insertAfter(this.$element);
//##!!
/*
var width = this._resolveWidth(this.$element, this.options.get('width'));
if (width != null) {
$container.css('width', width);
}*/
};
对于容器内的搜索:
Search.prototype.resizeSearch = function () {
//##!!
/*
this.$search.css('width', '25px');
var width = '';
if (this.$search.attr('placeholder') !== '') {
width = this.$selection.find('.select2-selection__rendered').innerWidth();
} else {
var minimumWidth = this.$search.val().length + 1;
width = (minimumWidth * 0.75) + 'em';
}
this.$search.css('width', width);
*/
};
return Search;
});
根据文档,您可以在初始化设置中添加适当的宽度:
$(".js-example-responsive").select2({
width: '100%' // or 'resolve' or 'style' or 'element' - see docs
});
对我来说就像一个魅力,而其他食谱没有帮助。
这对我有用
.select2-container {
width: 100% !important;
}
也是固定宽度
.select2-container {
width: 300px !important;
}
让它工作的黑客是运行:
$('.filter-modal select').css('width', '100%');
前
$('select').select2().
Select2 有一个奇怪的错误,它不能以正确的方式“解决”隐藏的 Select2 元素。它不起作用,width: "resolve"
因为这已经是默认值。相反,解决方法是显示 select2 并在声明后一秒钟突然隐藏它们。
$("#div_container_of_select2").show(); //make it visible for a millisecond
$("select[name=referee_type]").select2(); //declare Select2
$("#div_container_of_select2").hide(); //back to darkness
这将正确呈现 select2。
$("span.select2").css("width", "100%");