简体   繁体   中英

How to get value of data-subtext from Bootstrap Selectpicker?

I'm using Bootstrap-Select in my project and I'm running into issues where I can retrieve the selected value but cannot figure out how to retrieve the sub-text of that value. Let's use this dropdown for an example:

<select class="selectpicker" id="condimentPicker">
  <option data-subtext="Heinz">Ketchup</option>
  <option data-subtext="French's">Mustard</option>
  <option data-subtext="Hellmans">Mayo</option>
</select>

Let's assume that Ketchup is selected

In order to get the selected value of this dropdown, one would use var selectedItem = $('#condimentPicker').val(); . In this case, selectedItem = "Ketchup" , however I would want selectedItem = "Heinz" . Any idea how to do this? I've search through the documentation and couldn't seem to find anything:(

Found an answer. data-subtext is just a custom attribute, so you can do the following to fetch that value:

var selectedItem = $("#condimentPicker option:selected").attr("data-subtext");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM