简体   繁体   English

Select2占位符问题

[英]Select2 Placeholder Issue

I have a Select2 Multiselect html element.我有一个 Select2 Multiselect html 元素。 While rendering the placeholder is not displaying, but if I select and remove the item, the placeholder is displaying.渲染占位符时未显示,但如果我选择并删除该项目,则显示占位符。 I have no idea how to make it work.我不知道如何使它工作。 Please advise.请指教。

Initially最初

在此处输入图片说明

While Selection虽然选择

在此处输入图片说明

After removing the items取出物品后

在此处输入图片说明

在此处输入图片说明

Code:代码:

@Html.DropDownListFor(m => m.Vendor, new SelectList(Model.Status), "Search Status", new { @id = "advanced-search-lsc-status", @class = "form-control", @multiple = "multiple", data_placeholder = "Select Status" })


$('#advanced-search-status').select2({
            minimumResultsForSearch: -1,
            placeholder: function(){
                $(this).data('placeholder');
            },
            width: '100%'
        });

Just now found out that what was wrong, initially the width was set to 0 which hides the placeholder.刚刚发现有什么问题,最初将宽度设置为0,隐藏了占位符。

在此处输入图片说明

Just manipulated using this jquery code.只是使用这个 jquery 代码操作。

.select2-container--default .select2-search--inline .select2-search__field{
    width:initial!important;
}

now it works现在它起作用了

在此处输入图片说明

Thanks guys.谢谢各位。

try this试试这个

.select2-container .select2-search--inline:first-child {
    float: none;
}
.select2-container .select2-search--inline:first-child .select2-search__field{
    width: 100% !important;
}

Simply add attr placeholder只需添加 attr 占位符

$("#advanced-search-status").attr("placeholder", "Select Status");

or

$('#advanced-search-status').select2({
    placeholder: "Select Status"
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM