开发者

How do I retrieve the zero-based index of the selected option in a select box?

开发者 https://www.devze.com 2022-12-26 08:35 出处:网络
Let\'s say I have the following in my HTML code: <select name=\"Currency\" id=\"Currency\"> <option value=\"0.85\">Euro</option>

Let's say I have the following in my HTML code:

  <select name="Currency" id="Currency">
    <option value="0.85">Euro</option>
    <option value="110.33">Japanese Yen</option>
    <option value="1.2">Canadian Dollars</option>
  </select>

Using jQuery, I can use $("#Currency").val() to give me the selectd value, and I can开发者_如何学JAVA use $("#Currency :selected").text() to get the selected text.

What do I need to do to get the zero-based index (in this case, 0, 1, or 2) of the current selection?


You can get the selectedIndex attribute:

var index = $("#Currency").attr("selectedIndex");

Check an example here.


$("Currency").attr("selectedIndex")


document.getElementById("Currency").selectedIndex;


alternative method is to access the native javascript property.

$("#Currency")[0].selectedIndex
0

精彩评论

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