使用带有jQuery的索引获取HTML select选项的值

时间:2022-07-26 20:34:57

HTML for select and options:

用于选择和选项的HTML:

<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

我希望索引1处的选项值,类似于

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

3 个解决方案

#1


31  

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

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

eq()将从0开始,因为它是一个索引

Get the currently selected value with

获取当前选定的值

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

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

使用text()而不是val()来检索文本内容

#2


9  

To get the text:

要获得文本:

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

#3


2  

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

#1


31  

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

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

eq()将从0开始,因为它是一个索引

Get the currently selected value with

获取当前选定的值

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

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

使用text()而不是val()来检索文本内容

#2


9  

To get the text:

要获得文本:

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

#3


2  

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