Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
631 views
in Technique[技术] by (71.8m points)

javascript - 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 :(

question from:https://stackoverflow.com/questions/65910597/how-to-get-value-of-data-subtext-from-bootstrap-selectpicker

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...