fill select field when opening javascript / jquery page

1

Hi, I already received a lot of help when I needed a select field to fill in automatically in this post here: fill javascript / jquery select field

It's working perfectly well, but now I need to open a page where the select field is already filled, the second select field automatically pops up as well, but it's only filling the moment I click and select something in my first field select.

Here is the javascript code that fills the select (I need it to populate when I open the page, and a "selected" value will come from the database, not just when I select something)

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

})

    
asked by anonymous 21.05.2016 / 00:07

2 answers

1

You have to use the val parameter as well

$('select').selectpicker('val', 'valorDoOption');

This% of Boostrap% has its own methods, take a look at the documentation .

jsFiddle: link

    
21.05.2016 / 08:29
0

Use the selected attribute.

<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="vw">VW</option>
  <option value="audi" selected>Audi</option>
</select>
    
21.05.2016 / 01:10