Change selected item from select2 with ajax

Viewed 36

I want to use jquery to select the value of the desired item from select2.When I manually write the following code, it works correctly.

<!-- language: lang-html -->

<select class="form-control select2" id="voip">
    <option value="101001001">A</option>
    <option value="101001002">B</option>
    <option value="101001003">C</option>
    <option value="101001004">D</option>
</select>

$('#voip').val('101001003').trigger('change');

But when I receive the desired value with Ajax, it does not work. Please guide me, thank you

 $.ajax({
    url:"ajax_voip.php?internal=1",
    method:"GET",
    success:function(data)
    {
        $('#voip').val(data).trigger('change'); 
        alert(data);//this ok return 101001003
     }
})
1 Answers

it's possible your ajax call is returning a number and not a string... try:

$('#voip').val((""+data).trim()).trigger('change'); 
Related