select field when opening javascript / jquery page

Asked

Viewed 304 times

1

Hi, I had already received a great help when I needed a select field to be filled automatically, in this post here: Fill select javascript / jquery field

is working perfectly mind, but now I need that when opening a page where the select field is already filled, the second select field fills automatically too, but it is only filling in the moment I click and select something in my first select field.

Here is the javascript code that fills the select (I need it to fill in when I open the page, and a "Selected" value will come from the database, and 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');
        }
    )
})

})

2 answers

1


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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.