使用JQuery获取列表框中的所有列表项?

fai*_*gir 1 c# asp.net jquery

我想使用jquery获取列表框中存在的所有列表项,如果选择与否则无关紧要.

这是我的客户端脚本:

<asp:ListBox ID="lstSelected" runat="server" SelectionMode="Multiple" Rows="10">
    <asp:ListItem Value="Employee_OID">EID</asp:ListItem>
    <asp:ListItem Value="EmpID">Emp ID</asp:ListItem>
    <asp:ListItem Value="Employee_Name">Name</asp:ListItem>
</asp:ListBox>
Run Code Online (Sandbox Code Playgroud)

Hen*_*rik 6

通过向ListBox添加静态ID(ClientIDMode ="Static")生成,您可以直接在jQuery中使用ID属性值(跳过ClientID用法):

<asp:ListBox ID="lstSelected" runat="server" SelectionMode="Multiple" Rows="10" ClientIDMode="Static">
    <asp:ListItem Value="Employee_OID">EID</asp:ListItem>
    <asp:ListItem Value="EmpID">Emp ID</asp:ListItem>
    <asp:ListItem Value="Employee_Name">Name</asp:ListItem>
</asp:ListBox>
Run Code Online (Sandbox Code Playgroud)

jQuery获取所有选项:

$('#lstSelected option')
Run Code Online (Sandbox Code Playgroud)

jQuery获取所有选定的选项:

$('#lstSelected option:selected')
Run Code Online (Sandbox Code Playgroud)

在ClientIDMode上查看更多信息:http: //msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx