简体   繁体   English

无法预先选择选择值

[英]Unable to pre-select select value

I have a web page with a drop down list. 我有一个带有下拉列表的网页。 I have a JSFiddle of my problem here . 我的问题在这里 The drop-down list is rendered with the select2 plugin. 下拉列表使用select2插件呈现。 I need to support tags in the drop-down list. 我需要在下拉列表中支持标签。 My drop-down list works fine during the initial (create) state). 我的下拉列表在初始(创建)状态下工作正常。 However, when I am editing a record, I cannot figure out how to pre-populate the selected value. 但是,当我编辑记录时,我无法弄清楚如何预填充所选值。 I tried following the example from the docs which had the following: 我尝试按照以下文档中的示例操作:

$('#NameDisplay').select2({
  tags: ["Randy"]
}); 

However, as my Fiddle shows, it doesn't work. 但是,正如我的小提琴所显示的那样,它不起作用。 I included a "Basic" drop-down just to try to get something to populate. 我添加了一个“基本”下拉菜单,目的是尝试填充一些内容。 I also included a "Complete" to better show my entire scenario. 我还添加了“完成”以更好地展示我的整个情况。 I can't figure out what I'm doing wrong. 我不知道我在做什么错。

You can just set the value attribute of the <input> element. 您可以只设置<input>元素的value属性。 Set it to a comma-separated list of the tags. 将其设置为标记的逗号分隔列表。

<input class="form-control input-sm" id="NameDisplay"
       name="NameDisplay" type="text" value="Randy" />

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

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