JavaScript会找出从下拉列表中选择的内容

时间:2014-03-23 22:09:33

标签: javascript jquery

如何从用户从我的下拉列表中选择的内容中获取所选文本,这里是html

<select id="playerType" onchange="copy();">
<option value="0">Select one</option>
<option value="1">Goalkeepers</option>
<option value="2">Defenders</option>
<option value="3">Midfielders</option>
<option value="4">Forwards</option>

和JavaScript

function copy(){
var sel = document.getElementById("playerType");
var selection = sel.options[sel.selectedIndex].value;
if(selection == "goalkeeper"){
    textDoc = "goalkeepers.txt";
}
if(selection == "defender"){
    textDoc = "defenders.txt";
}
if(selection == "midfielder"){
    textDoc = "midfielders.txt";
}
if(selection == "forward"){
    textDoc = "forwards.txt";       
}
getAjaxData(textDoc);   

}

非常感谢您的帮助..

GMAN

3 个答案:

答案 0 :(得分:1)

使用:

var selection = sel.options[sel.selectedIndex].text;

而不是:

var selection = sel.options[sel.selectedIndex].value;

答案 1 :(得分:0)

您希望定位选择的innerHTML。像这样:

var somevar = document.getElementById('playerType');
alert(somevar.innerHTML);

如果您想在任何时候更改值,那么您可以执行以下操作:

somevar.onchange = function() {
    textDoc = somevar.innerHTML + '.txt';
}

答案 2 :(得分:0)

var selection = sel.options [sel.selectedIndex] .text;