繁体   English   中英

如何在下拉菜单中获取所选文本而不是值

[英]How to get the selected text instead of value in dropdown menu

我正在尝试为我的下拉菜单中的用户选择文本。

我有

   var selectMenu=document.createElement('select');
            selectMenu.className='menu';

        for(var i=0; i<array.length; i++){

           var option=document.createElement('option');
               option.className='option';
               option.innerHTML=array[i].name;
               option.value=array[i].id;

            selectMenu.appendChild(option);
         }

         $(selectMenu).change(function(){

           //i want to get the selected text here

           //I know I could get value by using $(this).val()
           //but not sure how to get the selected text here.

         })

我有谷歌这个问题,我发现的都是

$('#menu option:selected).text().

反正有没有得到我需要的东西? 非常感谢!

如果你有类似的东西

<select>
    <option value='1'> SO</option>
    <option value='2'>GOOGLE</option>
</select>

你可以试试

$("select").change(function(e){
    console.log($(":selected",this).text());
});

http://jsfiddle.net/Ykzp7/

尝试$('.menu option:selected).text()而不是$('#menu ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM