fill input multiselect select2

0

I am looking everywhere and I can not find how I can fill an input of a multiple select using select2.

the code I'm using is this:

if (ui.item.tallas >= 1) { $('#'+Talla).val(ui.item.talla.split(',')).trigger('change'); $('#'+Talla).select2("val", ui.item.talla.split(',')); } //compruebo que hay tallas

I call it 2 times to test if one of the two forms does it, but nothing neither one nor the other.

The selection I use is this:

<select tabindex="14" name="InPedido_Talla[]" id="InPedido_Talla" style="width: 80px;" class="multibuskiselect multibuskiselect_InPedido" multiple="multiple">

and the data received comes from mysql separated by a comma. that's why the split.

How could I insert those values into the input?

    
asked by Killpe 20.11.2017 в 22:23
source

1 answer

0

I do not know if I have understood your question well but this would be the closest thing I understand that you are looking to do, I hope it serves you, regards

$(document).ready(function() {

			var ui = {
			    item: {tallas: "15,16,34,32"}
			};

			var tallasAux = ui.item.tallas.split(',');

			if (tallasAux.length >= 1) { 

				for (var i = 0; i < tallasAux.length; i++) { 

					$('#selectTallas').append('<option value="'+i+'">'+tallasAux[i]+'</option>' );

				} 

			} 

});
#selectTallas{

  width:80px;

}
<!DOCTYPE html>
<html>
<head>
	<title></title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>

<select name="select" id="selectTallas"></select>

</body>
</html>
    
answered by 29.11.2017 в 00:43