我撞墙了!我的select2下拉框(位于图片中每行的中间)在jsfiddle中工作得非常漂亮但是当我将确切的代码传输到我的网页时,我遇到了两个奇怪的问题。首先,占位符名称从第4个dropbox开始,而不是从第一个Dropbox开始,几乎就像在某个地方有一个margin-top,它没有。
其次,下拉已经变得非常庞大而且过于分散。
除此之外,所有其他风格和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>
答案 0 :(得分:2)
解决!这表明了保证金的重要性:0px&#39;在相关的select2 CSS中。我发现:
.select2-container .select2-search--inline {
float: left;
margin: 0;
}
解决了占位符问题,而下拉列表问题解决了:
.select2-results__option {
padding: 5px;
user-select: none;
-webkit-user-select: none;
margin: 0px;
}
以前的保证金:0px;&#39;上面两个都没有。我认为它与收件箱中的select2复合范围有关,并且&#39;边距:0px&#39;需要取消这个。我把它留给那些比我聪明的人更好地解释。我仍然不明白为什么这一切都在jsfiddle中起作用,而不是在我的网页上。