如何在bootstrap select标签

时间:2017-05-10 06:52:31

标签: jquery select bootstrap-selectpicker

这是我的代码,但我无法获取selectpicker bootstraps中选择的值。知道怎么样?

<select class="selectpicker form-group show-tick" id ="department"  data-live-search="true" data-width="100%" multiple title = "Select Restaurant/Store - ID" data-max-options="1">
             <?php
                    while ($rstorefrom = mysqli_fetch_array($resultstorefrom))
                    {
                        echo "<option value = " .$rstorefrom['department']. "data-tokens=" .$rstorefrom['department'].">" .$rstorefrom['department']. "</option>";

                        echo "<script>
                          $('#department').on('click',function() {
                          alert($(this).val());
                          console.log($(this).val());
                        </script>";
                    } 


              ?>
            </select> 

1 个答案:

答案 0 :(得分:0)

您必须使用changed.bs.select等库事件来获取更多信息; https://silviomoreto.github.io/bootstrap-select/options/

$("#department").on("changed.bs.select",function(){
  console.log($(this).val())
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/js/bootstrap-select.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/css/bootstrap-select.min.css" rel="stylesheet"/>


<select class="selectpicker form-group show-tick" id ="department"  data-live-search="true" data-width="100%" multiple title = "Select Restaurant/Store - ID" data-max-options="1">
  <option value="1">Pizza</option>
    <option value="2">More Pizza</option>
      <option value="3">Big Pizza</option>
             </select>