I'm using Bootstrap 3.0.2 and the Bootstrap-select plugin.
Here's my select list:
<select class="selectpicker" data-live-search="true" data-size="7">
<option>Petr Karel</option>
<option>Honza Novák</option>
<option>David Egydy</option>
<option>Sláva Ková?</option>
<option>Hana Skalická</option>
<option>Simona Kolá?ová</option>
<option>Kate?ina Sychová</option>
<option>Amálka Sychová</option>
<option>Jana Sychová</option>
<option>Magdaléna Sychová</option>
<option>Tereza Sychová</option>
<option>Bohdana Sychová</option>
</select>
Here's my JavaScript:
//inicialization of select picker
$('.selectpicker').selectpicker();
//on change function i need to control selected value
$('select.selectpicker').on('change', function(){
var selected = $('.selectpicker option:selected').val();
alert(selected);
});
Issue
My problem is that change function won't work. It doesn't do anything, and I can't find solution by myself.
If i put it into document ready instead change function it seems to be working. So i guess i have mistake somewhere in:
$('select.selectpicker').on('change', function(){
I also tried to use only:
$('.selectpicker').on('change', function(){
without this select prefix but nothing changes.
UPDATE
Solution is putting jquery code into document.ready()
. Thanks to Kartikeya
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…