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
Add a ment  | 

2 Answers 2

Reset to default 7

Firstly, 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
Add a ment  | 

2 Answers 2

Reset to default 7

Firstly, 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