Javascript 无法在IE或Chrome中获取选择框的值?

Javascript 无法在IE或Chrome中获取选择框的值?,javascript,internet-explorer,google-chrome,Javascript,Internet Explorer,Google Chrome,我一直在FF中构建我的站点,但我刚刚意识到它在IE或Chrome中不起作用。使用IE的JS调试器,我发现它抛出了以下错误: SCRIPT5007: Unable to get value of the property 'value': object is null or undefined ... 对于以下代码: var myvar = document.getElementById("selectboxid").value; 它在FF中工作正常,但在IE或Chrome中则不行 选择框的H

我一直在FF中构建我的站点,但我刚刚意识到它在IE或Chrome中不起作用。使用IE的JS调试器,我发现它抛出了以下错误:

SCRIPT5007: Unable to get value of the property 'value': object is null or undefined ...
对于以下代码:

var myvar = document.getElementById("selectboxid").value;
它在FF中工作正常,但在IE或Chrome中则不行

选择框的HTML如下所示:

<select name="selectboxid" id="selectboxid" size="1" autocomplete="off" tabindex="5" >
<option value="1">One</option>
<option value="2">Two</option>
...
我做错什么了吗?如果是这样,为什么它在FF中工作良好

谢谢您的帮助。

您可以使用以下功能:

var myvar = document.getElementById("selectboxid");
var selectedValue = myvar.options[myvar.selectedIndex].value; //This will get the selected value of the select box
实现此功能的示例:

<html>
<head>
    <title>sample</title>
</head>
<body>
    <select name="selectboxid" id="selectboxid" onchange="alertValue()" size="1" autocomplete="off" tabindex="5">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
        <option value="4">Four</option>
    </select>
    <script type="text/javascript">
    function alertValue() //this function will only be called when the value of select changed.
    {
        var myvar = document.getElementById("selectboxid");
        var selectedValue = myvar.options[myvar.selectedIndex].value; //This will get the selected value of the select box
        alert(selectedValue);
    }
    </script>
</body>
</html>​
你可以用这个:

var myvar = document.getElementById("selectboxid");
var selectedValue = myvar.options[myvar.selectedIndex].value; //This will get the selected value of the select box
实现此功能的示例:

<html>
<head>
    <title>sample</title>
</head>
<body>
    <select name="selectboxid" id="selectboxid" onchange="alertValue()" size="1" autocomplete="off" tabindex="5">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
        <option value="4">Four</option>
    </select>
    <script type="text/javascript">
    function alertValue() //this function will only be called when the value of select changed.
    {
        var myvar = document.getElementById("selectboxid");
        var selectedValue = myvar.options[myvar.selectedIndex].value; //This will get the selected value of the select box
        alert(selectedValue);
    }
    </script>
</body>
</html>​


你也可以发布相关的HTML吗?像这样试试-@KevinBoucher-我添加了代码。我想@Jeff有你需要的答案。@KevinBoucher-也许。。我刚试过,但我有个错误。请看下面我的评论。你也可以发布相关的HTML吗?像这样试试-@KevinBoucher-我添加了代码。我想@Jeff已经找到了你需要的答案。@KevinBoucher-也许。。我刚试过,但我有个错误。请参阅下面的注释。我刚刚尝试了此操作,但出现了错误SCRIPT5007:无法获取属性“options”的值:对象为null或未定义…您使用的浏览器是什么?你什么时候调用javascript的?在select元素之前或之后?运行代码时,相关元素似乎不存在。Jeff的建议在Chrome的JSFIDLE中如期发挥作用。@JeffRobertDagala-我正在IE9和Chrome中尝试。javascript位于页面底部的html文件中。@KevinBoucher-选中复选框时代码将运行,因此代码运行时页面已加载很长时间。我刚刚尝试了此操作,并收到错误SCRIPT5007:无法获取属性“options”的值:对象为null或未定义…您使用的浏览器是什么?你什么时候调用javascript的?在select元素之前或之后?运行代码时,相关元素似乎不存在。Jeff的建议在Chrome的JSFIDLE中如期发挥作用。@JeffRobertDagala-我正在IE9和Chrome中尝试。javascript位于页面底部的html文件中。@KevinBoucher-选中复选框时代码将运行,因此代码运行时页面已加载很长时间。