使用jQuery在下拉列表中获取所选值。

时间:2010-08-18 14:57:32

标签: jquery jquery-selectors

我的HTML在下面。我需要使用。获取选定的值(Scheduled<select>标记。如何使用jQuery完成?

<select id="availability" style="display: none;">
  <option value="Available">Available</option>
  <option selected="selected" value="Scheduled">Scheduled</option>
  <option value="Unavailable">Unavailable</option>
</select>

我做了jQuery("#availability")来获取select标记,但我不知道如何获取所选选项的值。

6 个答案:

答案 0 :(得分:57)

尝试:

jQuery("#availability option:selected").val();

要获取该选项的文字,请使用text()

jQuery("#availability option:selected").text();

更多信息:

答案 1 :(得分:11)

以上解决方案对我不起作用。这是我最终提出的:

$( "#ddl" ).find( "option:selected" ).text();           // Text
$( "#ddl" ).find( "option:selected" ).prop("value");    // Value

答案 2 :(得分:8)

$("#availability option:selected").text();

这将为您提供下拉列表的文本值。您也可以使用.val()代替.text(),具体取决于您希望获得的内容。点击jQuery文档和示例的链接。

答案 3 :(得分:5)

我已经完成了上面提供的所有答案。 这是我用来从下拉列表中获取所选值的最简单方法

$('#searchType').val() // for the value

答案 4 :(得分:3)

$('#availability').find('option:selected').val() // For Value 
$('#availability').find('option:selected').text() // For Text
or 
$('#availability option:selected').val() // For Value 
$('#availability option:selected').text() // For Text

答案 5 :(得分:0)

大家好,我正在使用这种方法从所选的下拉列表列表中获取值,它就像魅力一样。

var methodvalue = $("#method option:selected").val(); 
相关问题