Get value of HTML select option using its index wi

2019-04-04 01:05发布

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'

3条回答
孤傲高冷的网名
2楼-- · 2019-04-04 01:41
$('.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

查看更多
来,给爷笑一个
3楼-- · 2019-04-04 01:54

To get the text:

var list = document.getElementById("dropdown");
var value = list.options[1].text;
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-04-04 01:58

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

查看更多
登录 后发表回答