如何在javascript中获取选定文本的值
可能重复:如何在javascript中获取选定文本的值,javascript,Javascript,可能重复: 我有一个选择: <select id="short_code"> <option value="12">First</option> <option value="11">Second</option> <option value="10">Third</option> <option value="9">Fourth</option> </select>
我有一个选择:
<select id="short_code">
<option value="12">First</option>
<option value="11">Second</option>
<option value="10">Third</option>
<option value="9">Fourth</option>
</select>
弗斯特
第二
第三
第四
我想获取所选文本的值。e、 g.如果所选文本是第一个
,那么我需要获得12
,请尝试以下操作:
document.getElementById('short_code').value
var el = document.getElementById("short_code");
var code = el.options[el.selectedIndex].value;
试试这个:
var el = document.getElementById("short_code");
var code = el.options[el.selectedIndex].value;
这应该做到:
<script type="text/javascript">
function getSelected(select) {
alert(select.options[select.selectedIndex].value);
}
</script>
<select id="short_code" onchange="getSelected(this)">
<option value="12">First</option>
<option value="11">Second</option>
<option value="10">Third</option>
<option value="9">Fourth</option>
</select>
函数getSelected(选择){
警报(select.options[select.selectedIndex].value);
}
弗斯特
第二
第三
第四
这应该可以做到:
<script type="text/javascript">
function getSelected(select) {
alert(select.options[select.selectedIndex].value);
}
</script>
<select id="short_code" onchange="getSelected(this)">
<option value="12">First</option>
<option value="11">Second</option>
<option value="10">Third</option>
<option value="9">Fourth</option>
</select>
函数getSelected(选择){
警报(select.options[select.selectedIndex].value);
}
弗斯特
第二
第三
第四