select2 MULTIPLE 占位符不起作用

问题描述 投票:0回答:14
javascript css twitter-bootstrap jquery-select2 placeholder
14个回答
27
投票

2020解决方案

这里的大问题是您选择的第一个选项 + 您的占位符太大而无法放入下拉列表中。因此,select2 将强制占位符为 width:0;.

这可以通过以下两行 CSS 来解决,这将覆盖此 select2“功能”:

.select2-search--inline {
    display: contents; /*this will make the container disappear, making the child the one who sets the width of the element*/
}

.select2-search__field:placeholder-shown {
    width: 100% !important; /*makes the placeholder to be 100% of the width while there are no options selected*/
}

注意

:placeholder-shown
伪类在某些浏览器的最旧版本以及 IE 上不起作用,因为您可以在here.


26
投票

经过数小时的尝试,我注意到最初呈现选择时,.select2-search__field 的宽度为 0px。当我选择一个选项然后删除该选项时,显示占位符并且宽度约为 1000px。

所以为了解决这个问题,我做了以下事情:

$('.search-select-multiple').select2({
    dropdownAutoWidth: true,
    multiple: true,
    width: '100%',
    height: '30px',
    placeholder: "Select",
    allowClear: true
});
$('.select2-search__field').css('width', '100%');

18
投票

在您的脚本文件中添加:

$('select').select2({
    minimumResultsForSearch: -1,
    placeholder: function(){
        $(this).data('placeholder');
    }
}):

在 HTML 中添加:

<select data-placeholder="Yours Placeholder" multiple>
    <option>Value 1</option>
    <option>Value 2</option>
    <option>Value 3</option>
    <option>Value 4</option>
    <option>Value 5</option>
</select>

9
投票

只需使用数据占位符而不是占位符。

<div class="form-group" style="width:70px; height:44px;">
    <select class="form-control select2" multiple="multiple" id="select2" data-placeholder="cawky parky">
        <option>ahoj</option>
        <option>more</option>
        <option>ideme</option>
        <option>na </option>
        <option>pivo?</option>
    </select>
</div>

1
投票

对我来说,占位符似乎需要

allowClear: true
和对象模式。尝试以下操作,看看它是否有效。有关使用 yadcf 的一般示例,请参见https://codepen.io/louking/pen/BGMvRP?#(抱歉,额外的复杂性,但我相信 yadcf 将 select_type_options 直接传递给 select2)。

$("#select2").select2({
    multiple:true,
    allowClear:true,
    placeholder: {
      id: -1
      text: "haha"
   }
});

1
投票

在你的脚本文件中:

$("select").each(function(i,v){
    var that = $(this);
    var placeholder = $(that).attr("data-placeholder");
    $(that).select2({
        placeholder:placeholder
    });
});

在您的 html 中(添加数据占位符 =“您的文本”):

<select data-placeholder="Yours Placeholder" multiple>
    <option>Value A</option>
    <option>Value B</option>
</select>

1
投票

这是 select2 GitHub 上的 已解决问题 但它从未在库本身中真正解决过。

我的解决方案类似于 Pedro Figueiredo 提出的 solution,除了它不会使容器消失。有一个关于display: contents

note
,这可能会导致一些可访问性问题。您也可以简单地将其切换为 100% 宽度:

.select2-search--inline,
.select2-search__field:placeholder-shown {
    width: 100% !important;
}

我更喜欢并发现基于

.select2-container
设置样式稍微更简洁,这样我就可以调用标准的 HTML 元素,如下所示:

.select2-container li:only-child,
.select2-container input:placeholder-shown {
  width: 100% !important;
}

任何一组代码都以任何一种方式访问相同的元素,我不确定哪一个更“未来证明”;现在这是一个偏好问题。

此外,如果您使用的是标签,则可能需要添加一个

.select2-container {
  width: 100% !important;
}

否则在切换到默认情况下不显示的选项卡时,select2 字段的宽度可能不正确。


0
投票

在你的 html 中

      <select class="form-control select2" multiple="multiple" id="select2">
             <option selected="selected">cawky parky</option>
             <option>ahoj</option>
             <option>more</option>
             <option>ideme</option>
             <option>na </option>
             <option>pivo?</option>   
    </select> 

在你的 jquery 中

$(".select2").select2({
    placeholder: "Selecione",
    allowClear: true
});

0
投票

我找到了另一种方法,当我将 select2 附加到 dom 时 $('.select2-search__field').width('100%') 用它改变宽度样式 $('.select2-search__field').width('100%')

function setSelect(select, data, placeholder) {
select.each(function () {
    let $this = $(this)
    $this.wrap('<div class="position-relative"></div>')
    $this.select2({
        placeholder,
        allowClear: true,
        dropdownParent: $this.parent(),
        dropdownAutoWidth: true,
        data
    })
})
$('.select2-search__field').width('100%')

}


0
投票

选择2/3.5.4

在你的 CSS 中:

.select2-search-field, .select2-search-field input[placeholder] {
  width: 100% !important;
}

0
投票

Select2 4.0.1 仍然不适用于我的多项选择。

我的解决方案

HTML

<select class="select-2" multiple>
   ...
</select>

JS

$('.select-2').select2({
    minimumResultsForSearch: -1,
    placeholder: function(){
        $(this).data('placeholder');
    }
});
$('.select-2[multiple]').each(function() {
   $(this).next('.select2').find('textarea').attr('placeholder', $(this).data('placeholder'));
});

$('.select2-search__field').css('width', '100%');

0
投票

试试这个代码

Open this image for the code

在你的javascript中

$('.select2-search__field').addClass('w-100')

$('.select2-search__field').css('width', '100%')

-1
投票

占位符标签不是选择列表的有效属性

你可以使用这样的东西:

       <select class="form-control select2" multiple="multiple" id="select2">
                 <option selected="selected">cawky parky</option>
                 <option>ahoj</option>
                 <option>more</option>
                 <option>ideme</option>
                 <option>na </option>
                 <option>pivo?</option>   
        </select> 

然后编写适当的 jquery

在你的select2-Bootstrap例子中是完全不同的结构


-1
投票

我在引导选项卡中有一些 select2 输入。 对我有用的解决方案:


.select2-search--inline {
    display: contents; /*this will make the container disappear, making the child the one who sets the width of the element*/
}

.select2-search__field:placeholder-shown {
    width: 100% !important; /*makes the placeholder to be 100% of the width while there are no options selected*/
}
© www.soinside.com 2019 - 2024. All rights reserved.