开发者

jQuery selected name

开发者 https://www.devze.com 2023-01-24 23:04 出处:网络
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?

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消