Insert multiple values into component

2

Well guys, I'm facing the following problem:

I have to set several values in one that is using the plugin below.

To set only one value I am using the following syntax

$("#s").select2('val',1);

Now how can I do to enter more than one value?

select2

    
asked by anonymous 25.04.2016 / 16:39

2 answers

0

For someone who needs to implement the following code and I got the result I wanted ..

var values = "1,2,3"

$.each(values.split(","),function(i,e){
   $("#select option[value='" + e + "']").prop("selected", true);
});

$("#select").trigger("change");
    
09.05.2016 / 21:12
2

You can do as follows using a%% of variables with select values, and if you want to select more than one item at a time in the dropdown and only add the data attribute to your select: / p>

HTML:

<select id="s" multiple="multiple">
</select>

JS:

var data = [
  { id: 0, text: 'enhancement', selected: true},
  { id: 1, text: 'bug' },
  { id: 2, text: 'duplicate' },
  { id: 3, text: 'invalid' }, 
  { id: 4, text: 'wontfix', selected: true}
];

$("#s").select2({
  data: data
});

In this object, id's represents the values of the select options, and text's

Link to test .

    
25.04.2016 / 16:43