如何从列表框项中获取文本?

时间:2011-05-04 12:02:49

标签: javascript listbox

当我尝试从javascript中的Listbox项获取文本时,它总是返回undefined。

winkeloptie.value = winkels[i][0]; 
winkeloptie.text = (winkels[i][0]);

alert(winkeloptie.value) ///This returns the value very nicely
alert(winkeloptie.text) //Returns undefined

我如何获取文字?

编辑:了解更多信息:

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); 
    }
}

2 个答案:

答案 0 :(得分:0)

这就是你要追求的吗? How to get Javascript Select box's selected text

答案 1 :(得分:0)

var x=document.getElementById("mySelect").selectedIndex;
var y=document.getElementById("mySelect").options;
alert("Index: " + y[x].index + " is " + y[x].text);