javascript 选定的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5420373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript selected value
提问by Siva
<div id="selected_options">
<select onchange="test()" id="selected_options">
<option value="0" selected>-Select-</option>
<option value="1">Communication</option>
<option value="2">Hardware</option>
</select>
</div>
i had written a function for selected value , but when i'm doing alert of selected value it showing up undefined , the function is
我已经为选定值编写了一个函数,但是当我对选定值发出警报时,它显示未定义,该函数是
function test() {
var get_id = document.getElementById('selected_options');
var result = get_id.options[get_id.selectedIndex].value;
alert(result);
}
any one please tell me what is the error?
任何人请告诉我什么是错误?
回答by u476945
You also have two id's with selected_options
. As this JSFiddlewould alert:
您还有两个带有selected_options
. 因为这个JSFiddle会提醒:
<div id="selected_options">
<select onchange="test()" id="selected_opt">
<option value="0" selected>-Select-</option>
<option value="1">Communication</option>
<option value="2">Hardware</option>
</select>
</div>
function test() {
var get_id = document.getElementById('selected_opt');
console.log(get_id);
var result = get_id.options[get_id.selectedIndex].value;
alert(result);
}
回答by dLobatog
The error is here:
错误在这里:
get_id.options[get_id.selectedIndex].value;
It should be
它应该是
get_id.value
And it will show up the selected value :)
它会显示选定的值:)
回答by Diodeus - James MacFarlane
get_id[get_id.selectedIndex].value;
"options" not required.
get_id[get_id.selectedIndex].value;
不需要“选项”。
回答by Dai
To get the currently selected value you can do:
要获取当前选定的值,您可以执行以下操作:
function test() {
var get_id = document.getElementById('selected_options');
var result = get_id.value;
alert(result);
}
回答by JWC
It's probably because your div and select tags have the same id.
这可能是因为您的 div 和 select 标签具有相同的 id。
回答by felixsigl
use jQuery its much easier:
使用 jQuery 更容易:
var result = $('#selected_options option:selected').val();
and use ids only once
并且只使用一次 ID
回答by KoolKabin
the problem in your code is the same id => 'selected_options' for both div and combobox.
您代码中的问题是 div 和组合框的 id => 'selected_options' 相同。
ids are meant for unique element representation in a page.
id 用于页面中的唯一元素表示。
you can check its live example here:
http://outsourcingnepal.com/projects/kool.htm
你可以在这里查看它的实例:http:
//outsourcingnepal.com/projects/kool.htm
code:
代码:
<script>
function test() {
var get_id = document.getElementById('selected_options');
var result = get_id.options[get_id.selectedIndex].value;
alert(result);
}
</script>
<h1>Outsourcing Nepal</h1>
<a href="http://www.outsourcingnepal.com">Home</a>
<div id="selected_options1">
<select onchange="test()" id="selected_options">
<option value="0" selected>-Select-</option>
<option value="1">Communication</option>
<option value="2">Hardware</option>
</select>
</div>