admin管理员组

文章数量:1399919

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"

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

<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>

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"

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

<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>
Share Improve this question asked Sep 12, 2012 at 21:58 NealRNealR 10.7k61 gold badges167 silver badges306 bronze badges 4
  • What does the actual rendered HTML look like? That would be much more useful seeing the script will also interact with that. It would show the actual rendered properties and so on. – Nope Commented Sep 12, 2012 at 21:59
  • 1 I'm hoping "everything you've read online" didn't include the .value after the getElementById() call... :) – Heretic Monkey Commented Sep 12, 2012 at 22:00
  • Where did you get the id AdvOrBasicSearch_advSearch_ddlState from? – Hamish Commented Sep 12, 2012 at 22:02
  • 2 I can barely read asp but this doesn't seem right: temp = document.getElementById("AdvOrBasicSearch_advSearch_ddlState").value. If I had to take an educated guess, I'd drop the .value – Jeremy J Starcher Commented Sep 12, 2012 at 22:02
Add a ment  | 

1 Answer 1

Reset to default 5

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

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

本文标签: aspnetGetting selected index from drop down list in javascriptStack Overflow