繁体   English   中英

Select2 —占位符不显示

[英]Select2 — placeholder not displaying

我在asp.net mvc 5应用程序中使用Select2插件。 根据文档

如果需要更大的灵活性,可以使用占位符选项传递数据对象而不是字符串。 数据对象的ID应与占位符选项的值匹配。

我确实做到了,但是占位符仍然没有出现。

码:

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"
    }
})

有人可以告诉我我在做什么错吗?

我认为占位符是一个字符串。 不是对象https://select2.github.io/examples.html#placeholders

$("select").select2({
    allowClear: true,
    placeholder:"Select an Title"

})

错误是placeholder: "Select an Title"应为-

$("select").select2({
    allowClear: true,
    placeholder: {
        id: "0",
        text: "Select an Title" //Should be text not placeholder
    }
})

https://select2.org/placeholders

我在这里写是因为我花了很多时间弄清楚代码中的错误。 我的占位符对象也没有显示(而占位符字符串显示正确)。 关键是空选项元素需要将该值与占位符ID匹配。 我的意思是,如果您输入第一个空选项,如下所示:

<option></option>

它与声明为的占位符不匹配

placeholder : { id:'0', text:'Please select...'}

它不会显示任何内容。 相反,您应该写:

 <option value="0"></option>

注意,如果使用了多重选择,则select2似乎需要将占位符作为对象。 有关使用yadcf的通用示例,请参见https://codepen.io/louking/pen/BGMvRP?# (抱歉,因为额外的复杂性,但我相信yadcf会将select_type_options直接传递给select2)。 (以下摘录)

  {
    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.

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