Refresh Select2 Jquery

2

I have a simple select2:

<select id="material" class="form-control select2 select2-hidden-accessible" style="width: 100%;"  aria-hidden="true">
   <option value="1">PC</option>
   <option value="2">Raton</option>
   <option value="3">Teclado</option>
   <option value="4">Monitor</option>
</select>

Your statement:

$('#material').select2();

The component works as expected.

The problem is that I change its value by:

$("#material").val("3");

And I check that its value changes with alert($("#material").val()); but it still shows the previous value.

That is, it does not update the component, how to solve this?

    
asked by nachfren 07.03.2017 в 10:39
source

1 answer

3

You should add after changing the value , a change() so that the value changes are affected visually in the Dropdown.

$("#material").val("3").change();

or

$("#material").val("3");
$("#material").change();
    
answered by 07.03.2017 / 10:54
source