Use selectpicker in jQuery

Asked

Viewed 852 times

1

I have this selectpicker code(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 fire a command every time the select selection is changed,but I have no idea how it does I thought of something like:

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

But it didn’t work,?

1 answer

1


To "fire a command every time the select selection is changed" you can use the event change 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>
<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>

Browser other questions tagged

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