views:

29

answers:

1

Hi,

<select name="my_field" onChange="my_field_change();">
  <option value="1" <% nvram_match("my_field", "1", "selected"); %>>Enabled</option>
  <option value="0" <% nvram_match("my_field", "0", "selected"); %>>Disabled</option>
</select>

...

function my_field_change()
{
   var my_value = document.forms[0].my_field.value;
   ...
   if (dhcp_relay == "1") {
       document.forms[0].some_other_field.disabled = 1;
   }
   ...
}

In the function "my_field_change()" I'm expecting to have 'my_value' equal to either 1 or 0, bit it won't happen. What am I doing wrong?

+1  A: 

You have to get the value associated with the selected index:

var my_value = document.forms[0].my_field[document.forms[0].my_field.selectedIndex].value;

If you can't get that to work, another option is to update the html to use an id, and reference that id specifically in the javascript:

<select id="my_field" name="my_field" onChange="my_field_change();">
  <option value="1" <% nvram_match("my_field", "1", "selected"); %>>Enabled</option>
  <option value="0" <% nvram_match("my_field", "0", "selected"); %>>Disabled</option>
</select>

...

function my_field_change()
{
   var select = document.getElementById("my_field");
   var my_value = select[select.selectedIndex].value;
   ...
}
JGB146
sorry for mistake, should be if (my_value == "1")...
Mark
JGB146, I tried your suggestion, but doesn't work unfortunately.
Mark
Is your `<select>` block inside a `<form>`? I tested this myself and it works for me as long as that is the case.
JGB146
@Mark, I updated with another option. Try that.
JGB146
@JGB146, the updated version doesn't work for me either, don't know what's going on with my system :(
Mark
@Eric Wendelin, yes the 'select' block is within 'form', I've double checked that.
Mark