在Internet Explorer中获取SELECT框的值 [英] Getting the value of a SELECT box in Internet Explorer
问题描述
我有一个选择框:
< select id =item1name =Item 1>
< option> < /选项>
< option>相机< / option>
< option>麦克风< / option>
< option>三脚架< / option>
< / select>
我有这样的JavaScript:
var item1 = document.getElementById(item1)。value;
item1
总是显示为空,选择。不过,这可以在 firefox 中找到。
使用item.value适用于所有浏览器,除了非常非常古老的浏览器(Netscape 4任何人?)。它在这种情况下不起作用的原因是因为您在选项中没有值属性。您应该为每个属性声明值。你目前拥有的只是文本属性,通常默认值为每当没有声明值时。或者,您可以在窗口onload事件中推送一些代码,以使每个选项的值与text相同。
下面,这是老式的方式: var s = document.getElementById('item1');
var item1 = s.options [s.selectedIndex] .value;
I have a select box:
<select id="item1" name="Item 1">
<option> </option>
<option> Camera </option>
<option> Microphone </option>
<option> Tripod </option>
</select>
And I have this JavaScript:
var item1= document.getElementById("item1").value;
item1
always shows empty, never the option selected. However, this works in firefox.
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屋!