Dol*_*osa 2 html css jquery jquery-select2
我正在使用 select2 创建一个多选字段,如下所示。
我想让占位符(“搜索项目”)总是在字段中看到,即使我已经选择了项目。在下拉列表中选择任何选项后,我尝试使用此方法放置新的占位符:
$(".select2-results__option").click(function() {
console.log("x");
$(document).find(".select2-search--inline .select2-search__field").attr("placeholder", "Search items");
});
Run Code Online (Sandbox Code Playgroud)
但不幸的是,它甚至不会触发控制台日志。
我的另一个问题是,我可以将药丸上的 ("x") 放在文本的右侧而不是左侧吗?
$(".select2-results__option").click(function() {
console.log("x");
$(document).find(".select2-search--inline .select2-search__field").attr("placeholder", "Search items");
});
Run Code Online (Sandbox Code Playgroud)
$(document).ready(function() {
$(".js-example-basic-multiple").select2({
placeholder: "Select items"
});
});
$(".select2-results__option").click(function() {
console.log("x");
$(document).find(".select2-search--inline .select2-search__field").attr("placeholder", "Search items");
});Run Code Online (Sandbox Code Playgroud)
.select2-selection {
height: 34px !important;
font-size: 13px;
font-family: 'Open Sans', sans-serif;
border-radius: 0 !important;
border: solid 1px #c4c4c4 !important;
padding-left: 4px;
}
.select2-selection--multiple {
height: 154px !important;
width: 366px !important;
}
.select2-selection__choice {
height: 40px;
line-height: 40px;
padding-right: 16px !important;
padding-left: 16px !important;
background-color: #CAF1FF !important;
color: #333 !important;
border: none !important;
border-radius: 3px !important;
}
.select2-search--inline .select2-search__field {
line-height: 40px;
color: #333;
}
.select2-container:hover,
.select2-container:focus,
.select2-container:active,
.select2-selection:hover,
.select2-selection:focus,
.select2-selection:active {
outline-color: transparent;
outline-style: none;
}
.select2-results__options li {
display: block;
}
.select2-selection__rendered {
transform: translateY(2px);
}
.select2-selection__arrow {
display: none;
}
.select2-results__option--highlighted {
background-color: #CAF1FF !important;
color: #333 !important;
}
.select2-dropdown {
border-radius: 0 !important;
box-shadow: 0px 3px 6px 0 rgba(0,0,0,0.15) !important;
border: none !important;
margin-top: 4px !important;
width: 366px !important;
}
.select2-results__option {
font-family: 'Open Sans', sans-serif;
font-size: 13px;
line-height: 24px !important;
vertical-align: middle !important;
padding-left: 8px !important;
}
.select2-results__option[aria-selected="true"] {
background-color: #eee !important;
}
.select2-search__field {
font-family: 'Open Sans', sans-serif;
color: #333;
font-size: 13px;
padding-left: 8px !important;
border-color: #c4c4c4 !important;
}
.select2-selection__placeholder {
color: #c4c4c4 !important;
}Run Code Online (Sandbox Code Playgroud)
小智 6
要在选择时显示占位符:
库input在选择时调整元素(带有占位符的元素)的大小。所以我们需要覆盖它。如何?这是一种方法:
if($(this).val() && $(this).val().length) {
$(this).next('.select2-container')
.find('li.select2-search--inline input.select2-search__field').attr('placeholder', 'Select items');
}
Run Code Online (Sandbox Code Playgroud)
在值选择时,它input从select2-container 中获取元素并显式添加一个占位符。
要在右侧显示删除图标:
无论如何,您有一堆覆盖 CSS,只需添加几行即可。:)
.select2-selection__choice__remove {
float: right;
margin-right: 0;
margin-left: 2px;
}
Run Code Online (Sandbox Code Playgroud)
这是代码片段:
if($(this).val() && $(this).val().length) {
$(this).next('.select2-container')
.find('li.select2-search--inline input.select2-search__field').attr('placeholder', 'Select items');
}
Run Code Online (Sandbox Code Playgroud)
.select2-selection__choice__remove {
float: right;
margin-right: 0;
margin-left: 2px;
}
Run Code Online (Sandbox Code Playgroud)
$(document).ready(function() {
$(".js-example-basic-multiple").select2({
placeholder: "Select items"
}).on('change', function(e) {
if($(this).val() && $(this).val().length) {
$(this).next('.select2-container')
.find('li.select2-search--inline input.select2-search__field').attr('placeholder', 'Select items');
}
});
});Run Code Online (Sandbox Code Playgroud)
希望这可以帮助