简体   繁体   中英

Hot to get select option value based on display name?

I have the following HTML select element:

<select id="publisher" name="publisher">
  <option value="10">Google</option>
  <option value="11">Yahoo</option>
  <option value="14">Facebook</option>
  <option value="17">Hotmail</option>
</select>

How to get option value based on display name, like i want 14 based on display name Facebook using Jquery or javascript.

var currentVal = $('#publisher').val(); //will return current selected value, numeric in your case
var facebookVal = $('#publisher').val('Facebook').val(); //will select facebook as current option and will fill facebookVal with value 14

Or, if you just wondering which value is for facebook, then:

var wonderVal = $('#publisher option:contains(Facebook)').val();

Hope this helped.

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