我想为网站制作一个下拉菜单,然后使用JavaScript获取用户决定的价值。我已经关注了网络上的其他资源;但它似乎没有用。它只是不断回复“选择一些东西......”#;;即使我改变了它。
<select id="dropDown" onChange="output()">
<option value="0">Select something...</option>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
<script>
var value = document.getElementById("dropDown");
var strUsr = value.options[value.selectedIndex].text;
function output(){
console.log(value);
}
output();
</script>
答案 0 :(得分:2)
它会为你工作
<select id="dropDown" onchange="output(this)">
<option value="0">Select something...</option>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
function output(dropDown)
{
alert(dropDown.options[dropDown.selectedIndex].innerHTML);
}
答案 1 :(得分:0)
这样做
<select id="dropDown" onChange="output(this)"> //added this here
<option value="0">Select something...</option>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
function output(sender){
alert(sender.options[sender.selectedIndex].text);
}
答案 2 :(得分:0)
如果您有一个如下所示的select元素:
<select id="dd">
<option value="1">t1</option>
<option value="2" selected="selected">t2</option>
<option value="3">t3</option>
</select>
运行此代码:
var e = document.getElementById("dd");
var strUser = e.options[e.selectedIndex].value;
strUser将是2.
var e = document.getElementById("dd");
var strUser = e.options[e.selectedIndex].text;
strUser将是t2