简体   繁体   English

Bootstrap 4 的 Select2 样式错误

[英]Error styling Select2 for Bootstrap 4

I'm working with Bootstrap 4, and for the <select> elements i want to use Select2 .我正在使用 Bootstrap 4,对于<select>元素,我想使用Select2

The problem is that it does not have a official Bootstrap 4 template, so i was looking and i found this project, which is awesome.问题是它没有官方的 Bootstrap 4 模板,所以我正在寻找并找到了这个项目,它很棒。

But i having one issue with is-invalid class.但是我遇到了一个is-invalid类的问题。

When i use it, the <select> borders change to red when the page loads and then return to its normal state当我使用它时, <select>边框在页面加载时变为红色,然后返回到正常状态

I try to make a simple class to use with the same Bootstrap color like我尝试制作一个简单的类来使用相同的 Bootstrap 颜色,例如

.invalid-select2 {
  border-color: #dc3545;
}

.invalid-select2:focus {
  border-color: #dc3545;
  box-shadow: 0 0 0 0.2rem rgba(220, 53, 69, 0.25);
}

But the result is the same;但结果是一样的;

You can see the problem in JSFIDDLE您可以在JSFIDDLE 中看到问题

Someone has idea how can i deal with it?有人知道我该如何处理?

Thanks so much!非常感谢!

The flikcering is because you're adding the class the the select box's class attribute so the style is applied immediately.闪烁是因为您将类添加到选择框的类属性中,以便立即应用样式。 Afterwards, select2 classes are applied which hides the select box (causing the flicker).之后,应用 select2 类隐藏选择框(导致闪烁)。

One way around this is to simply apply the class when the document is ready.解决此问题的一种方法是在文档准备好时简单地应用该类。

$(document).ready(function(){

    $('#combo').select2
  ({
    theme: 'bootstrap'
  });

  $("#combo + span").addClass("is-invalid");

});

EDIT: To fix the focus problem you're experiencing you need to over-ride a couple of classes.编辑:要解决您遇到的焦点问题,您需要覆盖几个类。

.is-invalid .select2-selection,
.needs-validation ~ span > .select2-dropdown{
  border-color:red !important;
}

.is-invalid .select2-selection selects the top part of the dropdown, and the second class ( .needs-validation ~ span > .select2-dropdown ) selects the actual dropdown list. .is-invalid .select2-selection选择下拉列表的顶部,第二类( .needs-validation ~ span > .select2-dropdown )选择实际的下拉列表。 Note that I've added .needs-validation to the top level div once the form is validated you can simply toggle that to was-validated请注意,一旦表单经过验证,我已将.needs-validation添加到顶级 div,您可以简单地将其切换为was-validated

https://getbootstrap.com/docs/4.0/components/forms/#validation https://getbootstrap.com/docs/4.0/components/forms/#validation

Here is a fiddle这是一个小提琴

I was able to use some CSS to style a red boarder我能够使用一些 CSS 来设计红色寄宿生的样式

.is-invalid + .select2-container--bootstrap .select2-selection--single {
    border: 1px solid #f44336;
}

您是否尝试将 !important 添加到 css 属性?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM