在 jQuery 中获取下拉列表的值

我有一个下拉列表,其中有一个“ ID,Name”对。

例子

乔恩 · 米勒
吉姆 · 史密斯
Jen Morsin

乔恩 · 米勒的身份证号是101
吉姆 · 史密斯的身份证号码是102
Jen Morsin 的身份是103

当我这样做的时候:

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

我选了乔恩 · 米勒,我得了101分,不过我还是想选乔恩 · 米勒。

358395 次浏览

$('#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();

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).

As has been pointed out ... in a select box, the .val() attribute will give you the value of the selected option. If the selected option does not have a value attribute it will default to the display value of the option (which is what the examples on the jQuery documentation of .val show.

you want to use .text() of the selected option:

$('#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()
var sal = $('.selectSal option:selected').eq(0).val();

selectSal is a class.

Try this:

var text = $('#YourDropdownId').find('option:selected').text();

Another option:

$("#<%=dropDownId.ClientID%>").children("option:selected").val();

This has worked for me!

$('#selected-option option:selected').val()

Hope this helps someone!

$('.leave_response').on('change', function() {
var responseId = $(this).val();
console.log(responseId);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td>
<select class="leave_response" name="leave">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</td>

Pass the id and hold into a variable and pass the variable where ever you want.

var temp = $('select[name=ID Name]').val();

You can have text value of #Crd in its variable .

$(document).on('click', '#Crd', function () {
var Crd = $( "#Crd option:selected" ).text();
});