开发者

get index of Select Option from Text value

开发者 https://www.devze.com 2023-03-21 01:02 出处:网络
I can\'t recall how to get the index of the option from the text without using a for loop. Is there an easy way?

I can't recall how to get the index of the option from the text without using a for loop. Is there an easy way?

Let's say I have a select like this:

<select id="prefix-select" name="survey[Prefix]" onchange="changeMade();">
  <option value="0">Mr.</option>
  <option value="1">Mrs.</option>
  <option value="2">Ms.</option>
  <option value="3">Dr.</option>
  <option value="4">Sr.</option>
  <option value="5">Sra.</option>
  <option value="6">Srta.</option>
</selec开发者_高级运维t>

Now I want to get the index when I have a text of '.Ms'


Jquery way:

var str = "Sr.";
index = $("#prefix-select > option:contains("+str+")").index())
0

精彩评论

暂无评论...
验证码 换一张
取 消