簡體   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