簡體   English   中英

Select 2 Multi Select 在選擇選項后從下拉列表中刪除選項

[英]Select 2 Multi Select remove option from the dropdown after selected the option

我正在使用 Jquery Select2 來進行帶復選框的多選。 我想從下拉列表中刪除選定的項目。 我正在使用下面的代碼。 它正在刪除該項目。 但是我在選擇項目后得到了輸入和下拉之間的差距。

我的代碼是 Javascript,

$('.js-select2'): {
            closeOnSelect: false,
            placeholder: "Placeholder",
            allowHtml: true,
            tags: true,
            allowClear: true,
            minimumResultsForSearch: -1,
        }

$("select").on("select2:select", function (evt) {
                var element = evt.params.data.element;
                var $element = $(element);
                $element.detach();
                $(this).append($element);
                $(this).trigger("change");
            });

CSS,

.select2-container {
  min-width: 100%;
}

.select2-results__option {
  padding-right: 20px;
  vertical-align: middle;
}
.select2-results__option:before {
  content: "";
  display: inline-block;
  position: relative;
  height: 20px;
  width: 20px;
  border: 2px solid #e9e9e9;
  background-color: #fff;
  margin-right: 20px;
  vertical-align: middle;
}
.select2-container--default .select2-selection--multiple {
    background-color: #FFFFFF;
    border: 1px solid #CBD5DD;
    border-radius: 2px;
    cursor: text;
    height: auto !important;
    margin: 0;
    min-height: 30px;
    overflow: hidden;
    padding: 2px;
    position: relative;
    width: 100%;
}

.select2-results__option[aria-selected=true] {
    display: none;
}
.select2-container--default .select2-results__option[aria-selected=true] {
    background-color: #fff;
}
.select2-container--default .select2-results__option--highlighted[aria-selected] {
    background-color: #3875d7;
    background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(20%, #3875d7), color-stop(90%, #2a62bc));
    background-image: -webkit-linear-gradient(#3875d7 20%, #2a62bc 90%);
    background-image: -moz-linear-gradient(#3875d7 20%, #2a62bc 90%);
    background-image: -o-linear-gradient(#3875d7 20%, #2a62bc 90%);
    background-image: linear-gradient(#3875d7 20%, #2a62bc 90%);
    color: #fff;
}
.select2-container--default .select2-selection--multiple {
    margin-bottom: 10px;
}
.select2-container--default.select2-container--open.select2-container--below .select2-selection--multiple {
    border-radius: 4px;
}
.select2-container--default.select2-container--focus .select2-selection--multiple {
    border: 1px solid #5897fb;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
}
.select2-container--default .select2-selection--multiple {
    border-width: 1px;
}
.select2-container--open .select2-dropdown--below {

    border-radius: 6px;
    box-shadow: 0 0 10px rgba(0,0,0,0.5);

}
.select2-selection .select2-selection--multiple:after {
    content: 'hhghgh';
}
/* select with icons badges single*/
.select-icon .select2-selection__placeholder .badge {
    display: none;
}
.select-icon .placeholder {
    display: none;
}
.select-icon .select2-results__option:before,
.select-icon .select2-results__option[aria-selected=true]:before {
    display: none !important;
    /* content: "" !important; */
}
.select-icon  .select2-search--dropdown {
    display: none;
}

.select2-container--default .select2-selection--multiple .select2-selection__choice {
    position: relative;
    margin: 3px 0 3px 5px;
    padding: 3px 3px 3px 5px;
    border: 1px solid #aaa;
    border-radius: 3px;
    background-color: #e4e4e4;
    background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(20%, #f4f4f4), color-stop(50%, #f0f0f0), color-stop(52%, #e8e8e8), color-stop(100%, #eeeeee));
    background-image: -webkit-linear-gradient(#f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
    background-image: -moz-linear-gradient(#f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
    background-image: -o-linear-gradient(#f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
    background-image: linear-gradient(#f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
    background-clip: padding-box;
    box-shadow: 0 0 2px white inset, 0 1px 0 rgba(0, 0, 0, 0.05);
    color: #333;
    line-height: 13px;
    cursor: default;
}

.select2-container--default .select2-selection--multiple .select2-selection__rendered li {
    list-style: none;
    width: auto !important;
}

.select2-remove-right {
  float: right;
}

.select2-container--default .select2-selection--multiple .select2-selection__choice__remove {
    float: right;
    margin-left: 5px;
}

ul.select2-choices {
    padding-right: 30px !important;
}

ul.select2-choices:after {
    content: "";
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    border-top: 5px solid #333;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
}

.select2-container--default .select2-search--inline .select2-search__field{
    width:initial!important;
}
.select2-dropdown {
  border-radius: 0px;
    background-color: white;
  border: 1px solid #DDD;
  box-sizing: border-box;
  display: block;
  position: absolute;
  left: -100000px;
  width: 100%;
  z-index: 1051;
}

/* line 43, ../scss/_dropdown.scss */
.select2-container--open .select2-dropdown--above {
  border-bottom: none;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

.select2-container--open .select2-dropdown--below {
  border-top: none;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

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

.select2-search--dropdown {
  display: block;
  padding: 7px;
}

.select2-search--dropdown .select2-search__field {
  padding: 4px;
  width: 100%;
  box-sizing: border-box;
}

.select2-search--dropdown .select2-search__field::-webkit-search-cancel-button {
  -webkit-appearance: none;
}

.select2-search--dropdown.select2-search--hide {
  display: none;
}

嘗試這個

https://stackoverflow.com/a/56195248/4208247

或這個

https://select2.org/selections#clearable-selections

可清除選擇設置為True時,會導致清晰的按鈕(“ X”圖標)出現在Z999938282F04071859941E18F16EFCF42Z BOX上時,選擇值。 單擊清除按鈕將清除所選值,有效地將 select 框重置為其占位符值。

$('select').select2({
  placeholder: 'This is my placeholder',
  allowClear: true
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM