简体   繁体   中英

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

I am trying to get the user selected texts for my dropdown menu.

I have

   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.

         })

I have google the issue and all I found are like

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

Are there anyways to get what I need? Thanks a lot!

if you have something like

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

you can try

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

http://jsfiddle.net/Ykzp7/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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