javascript 如何从列表框项中获取文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5883040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:40:10 来源:igfitidea点击:
How get the text from a listbox item?
提问by Jarco
When I try to get the text from a Listbox item in javascript it always returns undefined.
当我尝试从 javascript 中的列表框项中获取文本时,它总是返回未定义。
winkeloptie.value = winkels[i][0];
winkeloptie.text = (winkels[i][0]);
alert(winkeloptie.value) ///This returns the value very nicely
alert(winkeloptie.text) //Returns undefined
How can I get the text?
我怎样才能得到文本?
edit: for more info:
编辑:更多信息:
function addWinkels(){
var winkellijst = document.getElementById('winkel');
for (var i = 0;i < winkels.length;i++){
var winkeloptie = document.createElement("Option");
winkeloptie.text = (winkels[i][0]);
winkeloptie.title = (winkels[i][1]);
winkeloptie.value = winkels[i][0];
winkellijst.options.add(winkeloptie);
}
}
回答by Pav
var x=document.getElementById("mySelect").selectedIndex;
var y=document.getElementById("mySelect").options;
alert("Index: " + y[x].index + " is " + y[x].text);