Get value of HTML select option using its index with jQuery [closed]

瘦欲@ 提交于 2019-12-04 16:12:25

问题


HTML for select and options:

<select class="dropdown">
    <option value="one">One</option>
    <option value="two">Two</option>
    <option value="three">Three</option>
</select>

I want the value of the option at index 1, something along the lines of

$('.dropdown[1]').value
// should return 'two'

回答1:


$('.dropdown option').eq(1).val()

eq() will start with 0 as it is an index

Get the currently selected value with

$('.dropdown option:selected').val()

use text() instead of val() to retrieve the text content




回答2:


To get the text:

var list = document.getElementById("dropdown");
var value = list.options[1].text;



回答3:


You can use this to get the value:

$('select.dropdown option').eq(1).val();

An this to get the text:

$('select.dropdown option').eq(1).text();

Demo here



来源:https://stackoverflow.com/questions/18520594/get-value-of-html-select-option-using-its-index-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!