Get the value of a dropdown in jQuery

I have a drop down that has an 'ID, Name' Pair.

Example

Jon Miller
Jim Smith
Jen Morsin

Jon MIller has ID of 101
Jim Smith has ID of 102
Jen Morsin has ID of 103

When I do the followng:

var arNames = $('#Crd').val() 

and I select Jon Miller, I get 101. I'd like to get Jon Miller though.


$('#Crd').val() will give you the selected value of the drop down element. Use this to get the selected options text.

$('#Crd option:selected').text();

The best way is to use:

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

Depending on the requirement, you could also use this way:

var v = $("#yourid").val();
$("#yourid option[value="+v+"]").text()

If you're using a <select> , .val() gets the 'value' of the selected <option> . If it doesn't have a value , it may fallback to the id . Put the value you want it to return in the value attribute of each <option>

Edit: See comments for clarification on what value actually is (not necessarily equal to the value attribute).

链接地址: http://www.djcxy.com/p/29302.html

上一篇: 从文本中获取文本

下一篇: 在jQuery中获取下拉列表的值