fill field select javascript / jquery

1

Hello, I need to fill a select field with the return of this function, in this example, the variable RES returns:

<option value="">setor 1</option>

Just this, I need it to become one of the options in my select field, but nothing happens, I'm trying like this:

$('#cSetor').html(res);      

where cSetor is the ID of my select field

JavaScript:

$(document).ready(function() {
    $('#cSecretaria').on('change', function() {
        $.post('_require/_jp/jpcarregasetor.php', {
            id_sec: document.getElementById("cSecretaria").value,
        }, function(res) {
            $('#cSetor').html(res);
        })
    })
})

HTML:

<div class="form-group col-md-4">
    <label for="cSetor">Setor pertencente</label>
    <select class="form-control selectpicker" title="--" name="tSetor" id="cSetor">
    </select>
</div>
    
asked by anonymous 19.05.2016 / 22:10

3 answers

2

I see that you are using the Boostrap select, so for any changes you make you have to call the .selectpicker('refresh'); method to apply the changes.

If you want to subscribe to HTML you can use

$('#cSetor').html(res).selectpicker('refresh');

If you want to add HTML you can use

$('#cSetor').append(res).selectpicker('refresh');
    
19.05.2016 / 22:12
1

Change the "html" to "append":

$('#cSetor').append(res);
    
19.05.2016 / 22:38
0

Change value="", by value = ''

Also confirm that the ajax dataType is as HTML

    
19.05.2016 / 22:24