我正在尝试为我的下拉菜单中的用户选择文本.
我有
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.
})
Run Code Online (Sandbox Code Playgroud)
我有谷歌这个问题,我发现的都是
$('#menu option:selected).text().
Run Code Online (Sandbox Code Playgroud)
反正有没有得到我需要的东西?非常感谢!
如果你有类似的东西
<select>
<option value='1'> SO</option>
<option value='2'>GOOGLE</option>
</select>
Run Code Online (Sandbox Code Playgroud)
你可以试试
$("select").change(function(e){
console.log($(":selected",this).text());
});
Run Code Online (Sandbox Code Playgroud)