简体   繁体   中英

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. 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. Set it to a comma-separated list of the tags.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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