If Tom is selected from the drop down box, how do I get the name and not the value using jQuery so that the resulting output would be Tom?
<select id="emp" >
<op开发者_如何学运维tion value=1>Tom</option>
<option value=12>Harold</option>e
<option value=32>Jenny</option>
</select>
var res = $('#emp :selected').text();
This gets the element with the ID emp
, then gets the descendant element that is :selected
, finally returns the .tex()
content.
Here's a plain javascript version:
var el = document.getElementById('emp');
var res = el.options[el.selectedIndex].text;
$('#emp option:selected').text();
精彩评论