[英]Select2 — placeholder not displaying
I'm using Select2 Plugin in my asp.net mvc 5 application. 我在asp.net mvc 5应用程序中使用Select2插件。 according to the documentation 根据文档
The placeholder option allows you to pass in a data object instead of just a string if you need more flexibility. 如果需要更大的灵活性,可以使用占位符选项传递数据对象而不是字符串。 The id of the data object should match the value of the placeholder option. 数据对象的ID应与占位符选项的值匹配。
I have done exactly that, but the placeholder is still not showing up. 我确实做到了,但是占位符仍然没有出现。
Code: 码:
model.Step6.Titles.Insert(0, new SelectListItem() { Text = "Select a Title", Value = "0", Selected = true });
@Html.DropDownListFor(model => model.Step6.Title, Model.Step6.Titles, new { id="teamtitle", @style = "width: 100%;"})
$("select").select2({
allowClear: true,
placeholder: {
id: "0",
placeholder: "Select an Title"
}
})
can someone show me what I'm doing wrong here? 有人可以告诉我我在做什么错吗?
I think placeholder is a string. 我认为占位符是一个字符串。 Not an object https://select2.github.io/examples.html#placeholders 不是对象https://select2.github.io/examples.html#placeholders
$("select").select2({
allowClear: true,
placeholder:"Select an Title"
})
Error is placeholder: "Select an Title"
.It should be- 错误是placeholder: "Select an Title"
应为-
$("select").select2({
allowClear: true,
placeholder: {
id: "0",
text: "Select an Title" //Should be text not placeholder
}
})
https://select2.org/placeholders https://select2.org/placeholders
I write here because I've spent a lot of time figuring out what was wrong in my code. 我在这里写是因为我花了很多时间弄清楚代码中的错误。 I've got the placeholder object not showing too (whereas a placeholder string shows correctly). 我的占位符对象也没有显示(而占位符字符串显示正确)。 The point is that the empty option element needs to match the value to the placeholder id. 关键是空选项元素需要将该值与占位符ID匹配。 I mean, if you put a first empty option like the following: 我的意思是,如果您输入第一个空选项,如下所示:
<option></option>
it won't match with a placeholder declared as 它与声明为的占位符不匹配
placeholder : { id:'0', text:'Please select...'}
and it won't display anything. 它不会显示任何内容。 Instead you should write: 相反,您应该写:
<option value="0"></option>
Note if multi-select is used select2 seems to require the placeholder as an object. 注意,如果使用了多重选择,则select2似乎需要将占位符作为对象。 See https://codepen.io/louking/pen/BGMvRP?# for generic examples using yadcf (sorry for the extra complexity, but I believe yadcf passes select_type_options directly to select2). 有关使用yadcf的通用示例,请参见https://codepen.io/louking/pen/BGMvRP?# (抱歉,因为额外的复杂性,但我相信yadcf会将select_type_options直接传递给select2)。 (Excerpt below) (以下摘录)
{
column_number:2,
select_type: 'select2',
filter_type: 'multi_select',
select_type_options:
{
placeholder: {
id: -1,
text: 'pick col2'
},
width: '200px',
allowClear: true,
}
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.