admin管理员组文章数量:1241129
<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?
<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?
Share Improve this question edited Mar 24, 2011 at 14:18 KJYe.Name 17.2k5 gold badges50 silver badges63 bronze badges asked Mar 24, 2011 at 14:03 Siva Siva 3,7388 gold badges29 silver badges27 bronze badges7 Answers
Reset to default 6You also have two id's with selected_options
. As this JSFiddle would alert:
<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);
}
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 :)
get_id[get_id.selectedIndex].value;
"options" not required.
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);
}
It's probably because your div and select tags have the same id.
use jQuery its much easier:
var result = $('#selected_options option:selected').val();
and use ids only once
the problem in your code is the same id => 'selected_options' for both div and bobox.
ids are meant for unique element representation in a page.
you can check its live example here:
http://outsourcingnepal./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.">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>
本文标签: htmljavascript selected valueStack Overflow
版权声明:本文标题:html - javascript selected value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740071597a2223086.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论