Problems with <SELECT> and hidden elements!!

O

Omar

Hi,

In a JSP I have the next:

....
codigo = "<select name='" + nombre + "'>\n<option selected
value='default'>Escoge</option><option value='todos'>Todos</option>";
if (miRS != null)
while (miRS.next())
{
valorColumna = miRS.getString(nombre);
if (valorColumna.trim().equals(""))
do
{
miRS.next();
valorColumna = miRS.getString(nombre);
} while (valorColumna.trim().equals(""));
if (!valorColumna.trim().equals(""))
codigo = codigo + "<option value='" + valorColumna + "'>" +
valorColumna + "</option>";
} // fin del while
codigo = codigo + "</select><input type='hidden' name='valor" +
posicion + "' value='+this.options[this.selectedIndex].value+'>";
....

There, the hidden element should get the value of the selection made
by the user in the drop down menu.

In other JSP, I have to get this value. However, the value returned
is: +this.options[this.selectedIndex].value+

How can I get the selected item value?????

TIA
 
F

Fred Oz

Omar said:
Hi,

In a JSP I have the next:

...
codigo = "<select name='" + nombre + "'>\n<option selected
value='default'>Escoge</option><option value='todos'>Todos</option>";
if (miRS != null)
while (miRS.next())
{
valorColumna = miRS.getString(nombre);
if (valorColumna.trim().equals(""))
do
{
miRS.next();
valorColumna = miRS.getString(nombre);
} while (valorColumna.trim().equals(""));
if (!valorColumna.trim().equals(""))
codigo = codigo + "<option value='" + valorColumna + "'>" +
valorColumna + "</option>";
} // fin del while
codigo = codigo + "</select><input type='hidden' name='valor" +
posicion + "' value='+this.options[this.selectedIndex].value+'>";
...

There, the hidden element should get the value of the selection made
by the user in the drop down menu.

In other JSP, I have to get this value. However, the value returned
is: +this.options[this.selectedIndex].value+

Script in a form element's value attribute won't be executed, you
should only put text in there.

If you want to put the value of the selected option into the hidden
element, the best method is to use the form's onsubmit intrinsic
event. Rather than try to munge your script, here's a simple
example:

<form action="" onsubmit="
this.valorPos.value = this.nombre.value;
">
<select name="nombre">
<option value="default" selected>Select an option</option>
<option value="valorColumna0">Option 0</option>
<option value="valorColumna1">Option 1</option>
</select><br>
<input type="hidden" name="valorPos" value="default">
<input type="button" value="Show valorPos value" onclick="
alert(this.form.valorPos.value);
">
<input type="reset" onclick="
this.form.valorPos.value='default';
">
</form>

Note that while the reset button returns the hidden element's value
to 'default' in IE, it doesn't in Firefox, hence the 'onclick' code.
This may or may not be an issue for you.

You need to decide when the value needs to be put into the the hidden
input. Using an onchange has lots of issues with making sure the
value in the hidden input is aligned with what is shown in the
select.

In addition to the reset issue above, in Firefox and IE, re-loading
the page will return the value of the hidden input to 'default', but
the selected item of 'nombre' will not change.

As a note, it seems popular to get the value of a select thusly:

this.valorPos.value = this.nombre[this.nombre.selectedIndex].value;

but I think the first mentioned method should suffice. Any comments?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top