admin管理员组文章数量:1023838
Considering we have this HTML:
<select id="my_select">
<option value="1">Foo</option>
<option value="2">Bar</option>
<option value="">Bork</option>
<option value="3">Hey!</option>
</select>
The proper way to get the chosen value
would be:
var oS = document.getElementById("my_select");
alert(oS.options[oS.selectedIndex].value);
But if the third option, Bork, is chosen, the alert()
will show "Bork"
and not ""
(empty string).
How do I retrieve the empty string?
Considering we have this HTML:
<select id="my_select">
<option value="1">Foo</option>
<option value="2">Bar</option>
<option value="">Bork</option>
<option value="3">Hey!</option>
</select>
The proper way to get the chosen value
would be:
var oS = document.getElementById("my_select");
alert(oS.options[oS.selectedIndex].value);
But if the third option, Bork, is chosen, the alert()
will show "Bork"
and not ""
(empty string).
How do I retrieve the empty string?
Share Improve this question edited Sep 13, 2022 at 11:35 Chenmunka 8117 gold badges30 silver badges43 bronze badges asked Apr 8, 2011 at 17:48 kunambikunambi 7721 gold badge12 silver badges26 bronze badges 1- Can you not give Bork a value? – Richard Marskell - Drackir Commented Apr 8, 2011 at 17:51
2 Answers
Reset to default 7Firstly, it doesn't. For me, in Chrome and IE8, your example alerts an empty string (jsFiddle).
If, however, there is no value
set at all (jsFiddle), Bork
is alerted. This is, I think, the issue you're ing up against. This is correct behaviour. As the MDC page says,
If it is not defined, its default value is the text content of the element.
You could, however, use the getAttribute
method, which gives null
if no elements are selected (jsFiddle).
var oS = document.getElementById("my_select");
alert(oS.options[oS.selectedIndex].getAttribute('value'));
alert(document.getElementById("my_select").value)
returns the empty string, or any defined value of the selected option.
no need to specify options[selectedIndex]
If there is no default selected index, the first option value is returned.
If there is no value attribute for the selected option,
the option's text is returned in a script alert in most browsers,
and is sent to the server (if the select has a name and a form action) in all browsers.
Considering we have this HTML:
<select id="my_select">
<option value="1">Foo</option>
<option value="2">Bar</option>
<option value="">Bork</option>
<option value="3">Hey!</option>
</select>
The proper way to get the chosen value
would be:
var oS = document.getElementById("my_select");
alert(oS.options[oS.selectedIndex].value);
But if the third option, Bork, is chosen, the alert()
will show "Bork"
and not ""
(empty string).
How do I retrieve the empty string?
Considering we have this HTML:
<select id="my_select">
<option value="1">Foo</option>
<option value="2">Bar</option>
<option value="">Bork</option>
<option value="3">Hey!</option>
</select>
The proper way to get the chosen value
would be:
var oS = document.getElementById("my_select");
alert(oS.options[oS.selectedIndex].value);
But if the third option, Bork, is chosen, the alert()
will show "Bork"
and not ""
(empty string).
How do I retrieve the empty string?
Share Improve this question edited Sep 13, 2022 at 11:35 Chenmunka 8117 gold badges30 silver badges43 bronze badges asked Apr 8, 2011 at 17:48 kunambikunambi 7721 gold badge12 silver badges26 bronze badges 1- Can you not give Bork a value? – Richard Marskell - Drackir Commented Apr 8, 2011 at 17:51
2 Answers
Reset to default 7Firstly, it doesn't. For me, in Chrome and IE8, your example alerts an empty string (jsFiddle).
If, however, there is no value
set at all (jsFiddle), Bork
is alerted. This is, I think, the issue you're ing up against. This is correct behaviour. As the MDC page says,
If it is not defined, its default value is the text content of the element.
You could, however, use the getAttribute
method, which gives null
if no elements are selected (jsFiddle).
var oS = document.getElementById("my_select");
alert(oS.options[oS.selectedIndex].getAttribute('value'));
alert(document.getElementById("my_select").value)
returns the empty string, or any defined value of the selected option.
no need to specify options[selectedIndex]
If there is no default selected index, the first option value is returned.
If there is no value attribute for the selected option,
the option's text is returned in a script alert in most browsers,
and is sent to the server (if the select has a name and a form action) in all browsers.
本文标签: javascriptHow to retrieve empty OPTION value if it39s emptyStack Overflow
版权声明:本文标题:javascript - How to retrieve empty OPTION value if it's empty? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745547104a2155444.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论