Javascript selectedIndex未使用dropdownlist中的jQuery定义
我有一个ASP.NET下拉列表,如下所示:Javascript selectedIndex未使用dropdownlist中的jQuery定义,javascript,jquery,drop-down-menu,selectedindex,Javascript,Jquery,Drop Down Menu,Selectedindex,我有一个ASP.NET下拉列表,如下所示: <asp:DropDownList ID="ddlMyDropDown" runat="server"> <asp:ListItem>Please pick one</asp:ListItem> <asp:ListItem>option1</asp:ListItem> <asp:ListItem>option2</asp:ListItem>
<asp:DropDownList ID="ddlMyDropDown" runat="server">
<asp:ListItem>Please pick one</asp:ListItem>
<asp:ListItem>option1</asp:ListItem>
<asp:ListItem>option2</asp:ListItem>
<asp:ListItem>option3</asp:ListItem>
<asp:ListItem>option4</asp:ListItem>
</asp:DropDownList>
但在使用Firebug进行调试时,索引是未定义的。JQuery选择器查找select 35; contentplaceholder 1_ddlmy下拉列表
,因此这不是问题所在。selectedIndex
属性是否不存在
在互联网上,我找到了几乎完全相同的例子,而且效果很好。我对这件事很迷茫
更新
这就是Firebug所展示的:
正如您所看到的,
control
变量是某种数组,有一个条目实际上就是我想要在control
中的条目。我认为JQuery的ID选择器不会返回多个值?selectedIndex
不存在
您应该使用jquery的prop
var index = $("#ContentPlaceHolder1_ddlMyDropDown").prop('selectedIndex');
或
您可以使用asp.net属性检查此问题吗?@sarwar026我发现了问题所在,准备编辑问题。谢谢,我刚刚读到
$(“#id”)
与document.getElementById(“id”)不同。
请参阅注释。很明显,这是不一样的,看我的萤火虫截图。id选择器返回一个数组,如果由文档执行,它将返回一个DOM元素。getElementById
@MarioDeSchaepmeester jQuery始终使用数组。即使您认为自己有一个id,其他人也可以设置多个相同的id(糟糕的做法)——但jquery仍然可以使用它们。(第一个IMHO)这就是我不知道的。我是新手,看。奇怪的是,ID选择器也会这样做,因为每个人都会附加。get(0)
。。。
var index = $("#ContentPlaceHolder1_ddlMyDropDown").prop('selectedIndex');
var index = $("#ContentPlaceHolder1_ddlMyDropDown").get(0).selectedIndex;