【问题标题】:Select 2 Multi Select remove option from the dropdown after selected the option选择选项后从下拉列表中选择 2 多选删除选项
【发布时间】:2020-06-08 07:30:38
【问题描述】:

我正在使用 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;
}

【问题讨论】:

标签: javascript jquery jquery-select2 multi-select


【解决方案1】:

试试这个

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

或者这个

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

可清除的选择 设置为true时,会导致清除按钮(“x”图标)在选择值时显示在“选择”框上。单击清除按钮将清除所选值,有效地将选择框重置为其占位符值。

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

【讨论】:

    猜你喜欢
    • 2020-01-10
    • 2017-01-11
    • 2017-05-19
    • 1970-01-01
    • 2017-10-17
    • 2014-08-28
    • 1970-01-01
    相关资源
    最近更新 更多