下拉样式和占位符的 select2 问题

select2 problems with dropdown style and placeholders

我碰壁了!我的 select2 下拉框(位于图片中每一行的中间)在 jsfiddle 中工作得很好,但是当我将确切的代码传输到我的网页时,我遇到了 2 个奇怪的问题。首先,占位符名称从第 4 个保管箱而不是第一个保管箱开始,几乎就好像某处有边距顶部,但实际上没有。

其次,下拉菜单变得很大而且过于分散。

除此之外,所有其他样式和 jquery 都很好用!我为此花了 2 天时间,浏览了所有帖子,调整了所有 css 和 html,但什么也没做。任何人都可以提供任何想法吗?我使用的是 4.0.3 版,并在下面包含了一些重要的 css 和 jquery(抱歉长度)。

https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js

$( function() {

  $("#drop_ingot").select2({ placeholder: "Iron / steel types", });
  $("#drop_billetbloom").select2({ placeholder: "Iron / steel types", });
  $("#drop_slab").select2({ placeholder: "Iron / steel types", });
  $("#drop_rounds").select2({ placeholder: "Iron / steel types", });
  $("#drop_othersemis").select2({ placeholder: "Iron / steel types", });  
  $("#drop_rebar").select2({ placeholder: "Iron / steel types", });
  $("#drop_barrodcoil").select2({ placeholder: "Iron / steel types", });  
  $("#drop_barflats").select2({ placeholder: "Iron / steel types", });  

// This code ensures the drop menu disappears when deleting a tag.
$('[id^="drop_"]').on("select2:unselecting", function (e) {
    $(this).data('unselecting', true);
  }).on('select2:open', function(e) {
    if ($(this).data('unselecting')) {
      $(this).select2('close').removeData('unselecting');
    }
  });
});




.select2-container--default .select2-selection--multiple {
display: inline-block;
background-color: #FCFCFC;
border: 1px solid #DEDEDE;
border-radius: 5px;
cursor: text;
height: 24px;
padding: 0px 0px 0px 0px;
margin: 0px 2px 2px 1px;
cursor: pointer;
width:198px;
box-sizing: border-box;
user-select: none;
-webkit-user-select: none;
}

.select2-container .select2-search--inline {
float: left
}

.select2-container--default .select2-search--inline .select2-search__field {
box-sizing: border-box;
background: transparent; 
border: none;
font-size: 100%;
outline: 0;
margin-top: 4px;
padding: 0;
box-shadow: none;
-webkit-appearance: textfield
}

.select2-dropdown {
background-color: white;
border: 1px solid darkorange;
border-radius: 0px;
box-sizing: border-box;
display: block;
position: absolute;
font-family: 'Arial', Verdana;
font-size: 12px;
margin-left: 2px;
z-index: 100;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.select2-results {
display: block;
}

.select2-results__options {
list-style: none;
margin: 0;
padding: 0px;
}

.select2-results__option {
padding: 6px;
user-select: none;
-webkit-user-select: none
}

.select2-results__option[aria-selected] {
cursor: pointer
}

.select2-container--open .select2-dropdown {
left: 0px;
}

.select2-container--open .select2-dropdown--below {
border-top: none;
}

.select2-hidden-accessible {
border: 0 !important;
clip: rect(0 0 0 0) !important;
height: 1px !important;
margin: -1px !important;
overflow: hidden !important;
padding: 0 !important;
position: absolute !important;
width: 1px !important;
}

#drop_ptseamless + .select2-container--default .select2-selection--multiple{
height: auto;
min-height: 24px;
max-height: 45px;
padding: 0px 0px 3px 0px;
}

#drop_ptwelded + .select2-container--default .select2-selection--multiple{
  height: auto;
  min-height: 24px;
  max-height: 45px;
  padding: 0px 0px 3px 0px;
}

.tagContainerHoverClass:hover {
border: 1px solid darkorange !important;
background-color: white;
cursor: pointer;
}

.select2-container--default .select2-selection--multiple .select2-selection__rendered {
box-sizing: border-box;
list-style: none;
margin: 0;
padding: 0px 0px 0px 4px;
width: 100%;
}

.select2-container--default .select2-selection--multiple .select2-selection__choice {
color: black;
border-radius: 4px;
cursor: default;
float: left;
font-size: 12px;
margin: 3px 3px 0px 0px;
padding: 0 6px 0px 3px;
background:    #fff;
background:    -webkit-linear-gradient(#fff, #dadada);
background:    linear-gradient(#fff, #dadada);
border:        1px solid #878787;
box-shadow:    0 0px 0 0 #666;
text-shadow:   1px 1px 0 #f3f3f3;
}

.select2-container--default .select2-selection--multiple .select2-selection__choice__remove {
color: #5E5E5E;
cursor: pointer;
display: inline-block;
font-weight: bold;
margin-right: 3px
}

.select2-container--default .select2-selection--multiple .select2-selection__choice__remove:hover {
color: red;
font-size: 105%;
}

.select2-container--default .select2-results__option[aria-disabled=true] {
color: #999;
}

.select2-container--default .select2-results__option[aria-selected=true] {
color: red!important;
}

.select2-container--default .select2-results__option--highlighted[aria-selected] {
background-color: #CEDDF0;
}

.select2-container--default .select2-results__group {
cursor: default;
display: block;
padding: 6px
}

.select2-results__option[aria-selected=true] {
display: none;
}

.select2-container--default.select2-container--focus .select2-selection--multiple {
outline:none;
}







<div id="Lower_container_TradeProdsListBoxID_tr" class="Lower_container_TradeProdsListBoxClass_tr">


<div class = "TradeProds_RowProductsTitles">Steel semi-finished products</div>

<label><input type="checkbox"  id="tr_ingot" value="ingot" />Ingot</label>

<select id="drop_ingot" multiple="multiple">
<option value="Carbon">Carbon</option>
<option value="Stainless">Stainless</option>
<option value="Alloy">Alloy</option>
</select>

<a href="#" id="TradeProdsPOPUPLink_ingot_ID" class="RHLabelClass" >Trade codes</a>



<label><input type="checkbox"  id="tr_billetbloom" value="billetbloom" />Billet and Bloom</label>

<select id="drop_billetbloom" multiple="multiple">
<option value="Carbon">Carbon</option>
<option value="Stainless">Stainless</option>
</select>

<a href="#" id="TradeProdsPOPUPLink_billetbloom_ID" class="RHLabelClass" >Trade codes</a>



<label><input type="checkbox"  id="tr_slab" value="slab" />Slab</label>

<select id="drop_slab" multiple="multiple">
<option value="Carbon">Carbon</option>
<option value="Stainless">Stainless</option>
</select>

<a href="#" id="TradeProdsPOPUPLink_slab_ID" class="RHLabelClass" >Trade codes</a>



<label><input type="checkbox"  id="tr_rounds" value="rounds" />Rounds & other semis</label>

<select id="drop_rounds"  multiple="multiple">
<option value="Carbon">Carbon</option>
</select>

<a href="#" id="TradeProdsPOPUPLink_rounds_ID" class="RHLabelClass" >Trade codes</a>



<label><input type="checkbox"  id="tr_othersemis" value="othersemis" />Other semis</label>

<select id="drop_othersemis"  multiple="multiple">
<option value="Carbon">Carbon</option>
<option value="Alloy">Alloy</option>
</select>

<a href="#" id="TradeProdsPOPUPLink_othersemis_ID" class="RHLabelClass" >Trade codes</a>



<div class = "TradeProds_RowProductsTitles">Steel Finished Long Products</div>

<label><input type="checkbox"  id="tr_rebar" value="rebar" />Rebar / debar</label>

<select id="drop_rebar"  multiple="multiple">
<option value="Carbon">Carbon</option>
</select>

<a href="#" id="TradeProdsPOPUPLink_rebar_ID" class="RHLabelClass" >Trade codes</a>


<label><input type="checkbox"  id="tr_barrodcoil" value="barrodcoil" />Bar & wire rod (coiled)</label>

<select id="drop_barrodcoil"  multiple="multiple">
<option value="Carbon">Carbon</option>
<option value="Stainless">Stainless</option>
<option value="Alloy">Alloy</option>
</select>

<a href="#" id="TradeProdsPOPUPLink_barrodcoil_ID" class="RHLabelClass" >Trade codes</a>



<label><input type="checkbox"  id="tr_barflats" value="barflats" />HR bar & flats</label>

<select id="drop_barflats"  multiple="multiple">
<option value="Carbon">Carbon</option>
<option value="Stainless">Stainless</option>
<option value="Alloy">Alloy</option>
</select>

<a href="#" id="TradeProdsPOPUPLink_barflats_ID" class="RHLabelClass" >Trade codes</a>


<!---------------->

</div>

已解决!由此可见相关select2CSS中有'margin: 0px'的重要性。我发现:

.select2-container .select2-search--inline {
 float: left;
 margin: 0;
 }

解决了占位符问题,同时解决了下拉问题:

.select2-results__option {
padding: 5px;
user-select: none;
-webkit-user-select: none;
margin: 0px;

}

之前,'margin: 0px;' 在上述两个文件中都丢失了。我认为这与收件箱中的 select2 复合范围有关,并且需要 'margin: 0px' 来取消它。我把它留给比我聪明的人来解释得更好。我仍然不明白的是为什么这一切在 jsfiddle 中有效但在我的网页中无效。