[英]Getting displayed value of select tag
<select id="kamal">
<option value"ACTIVE">a<option>
<option value"DISABLED">b<option>
<option value"DELETED">c<option>
</select>
I want to get the value displayed on the page..not the value shown in the option tag我想获取页面上显示的值……而不是选项标签中显示的值
I am interested in "aktiv" not "ACTIVE"我对“aktiv”而不是“ACTIVE”感兴趣
when i write document.getElementById("kamal").value;
当我写
document.getElementById("kamal").value;
then the value that is select comes in the variable.然后 select 的值出现在变量中。 But I want the displayed value.
但我想要显示的值。
Please help me how can I take this value.请帮助我如何获取这个值。
NOTE: By using all the options given below, it will give me the value of the selected option, I want the label of the selected option.注意:通过使用下面给出的所有选项,它会给我所选选项的值,我想要所选选项的 label。 I mean the displayed value on html page.
我的意思是 html 页面上显示的值。
The solution you are looking for is:您正在寻找的解决方案是:
To get the value:获取值:
var element = document.getElementById("kamal");
var selectedValue = element.options[element.selectedIndex].value;
To get the text:要获取文本:
var element = document.getElementById("kamal");
var selectedValue = element.options[element.selectedIndex].text;
EDIT:编辑:
working example at:工作示例:
Try:尝试:
var sel = document.getElementById("kamal")
alert(sel.options[sel.selectedIndex].value);
Note: your <option>
tags should be closed with </option>
注意:您的
<option>
标签应该用</option>
结束
var element = document.getElementById("kamal"); var element = document.getElementById("kamal"); var selectedValue = element.options[element.selectedIndex].
var selectedValue = element.options[element.selectedIndex]。 innerHTML;
内部HTML;
THis was the solution of my question.这是我的问题的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.