在 Internet Explorer 中获取 SELECT 框的值 [英] Getting the value of a SELECT box in Internet Explorer

查看:32
本文介绍了在 Internet Explorer 中获取 SELECT 框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择框:

<select id="item1" name="Item 1">
  <option> </option>
  <option> Camera </option>
  <option> Microphone </option>
  <option> Tripod </option>
</select>

我有这个 JavaScript:

And I have this JavaScript:

var item1= document.getElementById("item1").value;

item1 总是显示为空,从不选择选项.但是,这适用于 .

item1 always shows empty, never the option selected. However, this works in firefox.

推荐答案

使用 item.value 适用于所有浏览器,除了非常非常旧的浏览器(Netscape 4 任何人?).在这种情况下它不起作用的原因是因为您在选项中没有 value 属性.您应该为每个属性声明值.您目前拥有的只是文本"属性,通常在没有声明任何值时默认为值.或者,您可以在窗口 onload 事件中推送一些代码,以使这些选项中的每一个的值"与文本"相同.

Using item.value works for all browsers except very very old ones (Netscape 4 anyone?). The reason it does not work in this case is because you have no value attribute in options. You should declare value for each attribute. What you currently have is only "text" property, which normally defaults to value whenever no value is declared. Alternately you can push some code in the window onload event to make "value" of each of these options same as "text".

您可以使用以下代码的第三种方式,这是老式的方式:

A third way you can use the code below, which is the old-fashioned way:

var s = document.getElementById('item1');
var item1 = s.options[s.selectedIndex].value;

这篇关于在 Internet Explorer 中获取 SELECT 框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆