admin管理员组文章数量:1178537
I need to print the selected option ID with Javascript not JQuery for both select tags.
Assume we have more than one select tags.
<select onchange="showOptions(this)" id="my_select1">
<option value="a1" id="ida1">Option1</option>
<option value="a2" id="ida2">Option2</option>
</select>
<select onchange="showOptions(this)" id="my_select2">
<option value="b1" id="idb1">Option1</option>
<option value="b2" id="idb2">Option2</option>
</select>
I found out the following way options[selectedIndex].id
but how can I know to which one of those that line refers to..
Any suggestions?
I Tried the following but it did not work.
<select id="my_select" onchange="showOptions2(this)">
<option value="o1" id="id1">Option1</option>
<option value="o2" id="id2">Option2</option>
</select>
<script type = "text/javascript">
function showOptions2(s){
var adVALUE = console.log(s[s.selectedIndex].value); // get value
var adID = console.log(s[s.selectedIndex].id); // get id
alert(adID);
}
</script>
I need to print the selected option ID with Javascript not JQuery for both select tags.
Assume we have more than one select tags.
<select onchange="showOptions(this)" id="my_select1">
<option value="a1" id="ida1">Option1</option>
<option value="a2" id="ida2">Option2</option>
</select>
<select onchange="showOptions(this)" id="my_select2">
<option value="b1" id="idb1">Option1</option>
<option value="b2" id="idb2">Option2</option>
</select>
I found out the following way options[selectedIndex].id
but how can I know to which one of those that line refers to..
Any suggestions?
I Tried the following but it did not work.
<select id="my_select" onchange="showOptions2(this)">
<option value="o1" id="id1">Option1</option>
<option value="o2" id="id2">Option2</option>
</select>
<script type = "text/javascript">
function showOptions2(s){
var adVALUE = console.log(s[s.selectedIndex].value); // get value
var adID = console.log(s[s.selectedIndex].id); // get id
alert(adID);
}
</script>
Share
Improve this question
edited Sep 27, 2012 at 16:15
yorgos
asked Sep 27, 2012 at 16:03
yorgosyorgos
1,8186 gold badges22 silver badges29 bronze badges
1
|
1 Answer
Reset to default 35<select onchange="showOptions(this)">
...
this function will do the work
function showOptions(s) {
console.log(s[s.selectedIndex].value); // get value
console.log(s[s.selectedIndex].id); // get id
}
Note that, unless you are using them for other purpose, you may omit the id
on select
elements
Example jsbin : http://jsbin.com/adopiz/2/edit
本文标签: htmlHow to get selected option ID with Javascript not JQueryStack Overflow
版权声明:本文标题:html - How to get selected option ID with Javascript not JQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738083390a2060796.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
id
. – Some Guy Commented Sep 27, 2012 at 16:04