开发者

get value of a list

开发者 https://www.devze.com 2023-01-11 01:42 出处:网络
>Enabled >Disabled ... function my_field_change() { var my_value = document.forms[0].my_field.value; ... if (dhcp_relay == \"1\") {

>Enabled >Disabled

...

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?


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;
   ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消