简体   繁体   中英

Set Selected Value from array using select2 javascript

I have array in javascript like this

var array = ["Impression", "Clicks", "CTR", "CVR", "CPC", "CPM", "CPA"]

I want to set selected text in select2 from one of my array but getting confused.

select2 code:

 $('.params_1').select2({ placeholder: "Impression", allowClear: false, data: array, initSelection: true }); 
 <select> <option selected="selected">text</option> <option>text</option> </select> 

I'm using placeholder :"impression" but that not selected, it just label in my select option.

If you want have some option to be selected from array, your html should contain that option.

For example- if you want to set "Impression" to be selected, change your html to following-

<select>
  <option selected="selected">Impression</option>
</select>

And if you want to provide your options with some value , you should change your Array to be an array of objects. For more details, please check this link.

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