Select2 组件如果是必填项,则边框颜色。

huangapple go评论65阅读模式
英文:

Select2 border color if select component is required

问题

我正在为输入字段添加边框颜色,但无法将其应用于select2组件。

以下是您的CSS代码部分,已翻译:

input:required.form-control:focus, textarea:required.form-control:focus, select:required.form-control:focus{
    border-color: #FF0000!important;
    box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(255, 0, 0, 0.6) !important;
}
input:required:invalid, textarea:required:invalid, select:required:invalid {border: 1px solid darkorange!important;}
input:required:valid, textarea:required:valid, select:required:valid { border: 1px solid black!important; }

这是您的select2代码:

<select id="select" required>
  <option value="">Select</option>
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="3">Option 3</option>
  <option value="4">Option 4</option>
</select>

<script>
  $(document).ready(function() {
    $('#select').select2();
  });
</script>

您希望在select2组件需要时更改边框颜色。当选择了该组件中的某项(除了"Select"之外的任何选项)时,将边框颜色更改为黑色。这就是您的需求。这是您模板的JSFiddle链接

英文:

I'm adding border color to input fields but can't apply this to select2 components.

<pre>
input:required.form-control:focus, textarea:required.form-control:focus, select:required.form-control:focus{
border-color: #FF0000!important;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(255, 0, 0, 0.6) !important;
}
input:required:invalid, textarea:required:invalid, select:required:invalid {border: 1px solid darkorange!important;}
input:required:valid, textarea:required:valid, select:required:valid { border: 1px solid black!important; }
</pre>

This is my select2 codes:

&lt;select id=&quot;select&quot; required&gt;
  &lt;option value=&quot;&quot;&gt;Select&lt;/option&gt;
  &lt;option value=&quot;1&quot;&gt;Option 1&lt;/option&gt;
  &lt;option value=&quot;2&quot;&gt;Option 2&lt;/option&gt;
  &lt;option value=&quot;3&quot;&gt;Option 3&lt;/option&gt;
  &lt;option value=&quot;4&quot;&gt;Option 4&lt;/option&gt;
&lt;/select&gt;


&lt;script&gt;
  $(document).ready(function() {
  $(&#39;#select&#39;).select2();
  });
&lt;/script&gt;

Just want to make border color of select2 component if it's required. And when something selected in that component (anything but "Select" as you can guess) let the color be black. That's it.

This is JSFiddle link of my template.

答案1

得分: 2

我修改了你的fiddle代码
在fiddle https://jsfiddle.net/z2k5roLy/1/

这些CSS行有助于

select.select2:required + .select2-container .select2-selection--single {
border-color: #FF0000;
}

select.select2:required:valid + .select2-container .select2-selection--single {
border-color: black;
}

英文:

I modified your fiddle code
in fiddle https://jsfiddle.net/z2k5roLy/1/

those css lines helps

select.select2:required + .select2-container .select2-selection--single {
  border-color: #FF0000;
}

select.select2:required:valid + .select2-container .select2-selection--single {
  border-color: black;
}

huangapple
  • 本文由 发表于 2023年4月19日 18:59:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定