2016-08-25 176 views
0

編號:https://silviomoreto.github.io/bootstrap-select/options/引導選擇 - 獲得選擇的值

進出口使用上述我的網頁上,讓我的用戶選擇多個值。 但是我想簡單地顯示所選值的文本,但是要麼不可能,要麼我做的都是錯的? 我得到的所有價值,並希望「入選」值 當我想要顯示的結果給用戶以後...

<div class="form-group"><label class="col-lg-2 control-label">Service(s):</label> 
    <div class="col-lg-9"> 
    <select class="selectpicker" multiple title="Select from the following..." data-width="100%" id="serviceList"> 
    </select></div></div> 

    <script> 
    var services = [{"value":"3e197221-e48b-44b8-a2aa-2054638f3bbc","label":"Service1","price":30.000000},{"value":"cd909812-ff6e-484b-96e3-c344ba20e140","label":"Service2","price":10.000000}]; 
    $('.selectpicker').on('changed.bs.select', function (e, clickedIndex, newValue, oldValue) { 
      var v = $(".selectpicker option:selected").val(); 
      var result =[]; 
      for (var i = 0; i < services.length; i++){ 
       // look for the entry with a matching value 
       if (services[i].value === v) { 
        result.push(services[i].label); 
        console.log(services[i].label); 
       } 
      } 
      for (i = 0; i < result; i++) { 
       console.log(result[i]); 
      } 

     }); 
    </script> 

回答

0

這個工作,似乎長篇大論。

var selectedServices =[]; 
$('.selectpicker').on('changed.bs.select', function (e, clickedIndex, newValue, oldValue) { 



     for (var key in services) { 
      if (key == clickedIndex) { 
       console.log(key + ' is ' + services[key]); 
       selectedServices.push(services[key].label); 
       console.log(services[key].label); 
      } 

     } 
2

您想要從所選選項陣列中「拔取」所有標籤。

$('.selectpicker').on('changed.bs.select', function() { 

    selectedServices = $.map($(this).find("option:selected"), function(o) { return o["label"]; }); 

}