javascript 从javascript的下拉列表中获取选定的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12396710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:05:42  来源:igfitidea点击:

Getting selected index from drop down list in javascript

javascriptasp.netdrop-down-menu

提问by NealR

Everything I've read online says this is the method and syntax I need to use to get the selected index from a drop down list.

我在网上阅读的所有内容都说这是我从下拉列表中获取所选索引所需的方法和语法。

var temp;
temp = document.getElementById("AdvOrBasicSearch_advSearch_ddlState").value;
var sState = temp.options[temp.selectedIndex].text;

However, I get the following error on the last line:

但是,我在最后一行收到以下错误:

"Microsoft JScript runtime error: 'options' is null or not an object"

“Microsoft JScript 运行时错误:‘options’为空或不是对象”

Below is a sampling of the drop down list (no need to post all 50 states)

以下是下拉列表的示例(无需发布所有 50 个州)

<td><asp:dropdownlist id="ddlState" tabIndex="8" runat="server" EnableViewState="False" Width="150px"
    CssClass="clsTextInput">
    <asp:ListItem Value=""></asp:ListItem>
    <asp:ListItem Value="AL">Alabama</asp:ListItem>
    <asp:ListItem Value="AK">Alaska</asp:ListItem>
</asp:dropdownlist></td>

回答by NealR

Thanks to the comments I dropped the .value and everything works fine. Thx guys!

感谢评论,我删除了 .value 并且一切正常。伙计们!

var temp;
temp = document.getElementById("AdvOrBasicSearch_advSearch_ddlState");
sState = temp.options[temp.selectedIndex].text;