我正在设计一个这样的网页:
<html>
<head>
<title>Bug UI</title>
</head>
<body>
<script>
function myfunc()
{
//what goes here??
}
</script>
<form>
<select name = "parameters">
<option value = "param1">Param 1</option>
<option value = "param2">Param 2</option>
<option value = "param3">Param 3</option>
<option value = "param4">Param 4</option>
<option value = "param5">Param 5</option>
</select>
<input type = "button" onclick = "myfunc()" value = "Submit">
</form>
</body>
</html>
它显示一个下拉框,当我从框中选择一个值(比如Param 1)并单击“提交”时,我需要打印该值(在这种情况下为Param 1)。怎么做到这一点?
答案 0 :(得分:6)
var s = document.getElementsByName('parameters')[0];
var text = s.options[s.selectedIndex].text;
答案 1 :(得分:1)
完美地尝试这项工作:
var ex = document.getElementsByTagName('select');
var str= ex.options[ex.selectedIndex].value;
**or**
var str= ex.options[ex.selectedIndex].text;
或强>
var ex = document.getElementsByName('parameters')[0];
var str= ex.options[ex.selectedIndex].value;
**or**
var str= ex.options[ex.selectedIndex].text;
答案 2 :(得分:0)
试试这个:
function myfunc() {
var par=document.getElementsByName('parameters')[0];
var index=par.selectedIndex
console.log(par.options[index].text);
}