2012-10-22 62 views
1

我想使用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> 
+0

由於您使用的是傳統的.Net(runat =「server」),因此請小心數據的時間範圍。像其他人所建議的那樣只會是好的「POST」FACTO – Pinch

回答

5

通過添加靜態ID(的ClientIDMode = 「靜態」)的產生到列表框,你可以用ID屬性值jQuery的直接(跳過客戶端ID使用):

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

jQuery來得到所有選項:

$('#lstSelected option') 

jQuery來獲取所有選擇的選項:

$('#lstSelected option:selected') 

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

+0

這麼做了......我需要csv格式的值和文本......任何幫助? – faizanjehangir

+0

如果您搜索stackoverflow,可以找到更好的答案,但請查看.val()和.text()方法以獲取選項的值和文本。例如:http://stackoverflow.com/questions/880293/jquery-get-remaining-option-values-in-csv-format – Henrik

1

爲了讓你將需要使用ListBoxClientID屬性的元素客戶端:

所有選項

$("#<%=lstSelected.ClientID %> option"); 

所有選定的選項

$("#<%=lstSelected.ClientID %> option:selected"); 
0

我想這應該做的伎倆:

$('#lstSelected option').each(function(){ 


if(this.val() == anyvalue) 

//your code here 

}); 
+0

我使用了這個功能,它不會執行... – faizanjehangir

0
$("#<%= lstSelected.ClientID %>").find("option"); 
1

我是不是能夠對neurotix的回答原因發表評論聲譽..想分享,雖然這對我工作只需要把$()圍繞「這個」。

$('#lstSelected option').each(function(){ 


if($(this).val() == anyvalue) 

//your code here 

}); 
相關問題