Use selectpicker in jQuery

1

I have this code of selectpicker (bootstrap)

<select name="nivel_p" class="selectpicker">
    <option>menor que 6</option>
    <option>7-15</option>
    <option>16-40</option>
    <option>maior que 40</option>
</select>

I need to trigger a command every time the selection of the select is changed, but I do not have the slightest idea of how it did I thought of something like:

$('select[name=nivel_p]').selectpicker(function() {
}

But it did not work, what is the right way to do it?

    
asked by anonymous 24.11.2014 / 23:52

1 answer

1

To "trigger a command every time the selection of the select is changed" can cause the change event to be used and the code would look like this:

$('select[name=nivel_p]').on('change', function(){
    // correr código aqui
});

Example:

$('select[name=nivel_p]').on('change', function(){
    alert('A opção selecionada mudou!\nO novo valor é: ' + $(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><selectname="nivel_p" class="selectpicker">
    <option>menor que 6</option>
    <option>7-15</option>
    <option>16-40</option>
    <option>maior que 40</option>
</select>
    
25.11.2014 / 03:33