如何使用jquery获取选定的Asp.net checkBoxList项目的索引及其文本值
如何使用jQuery获取所选ASP.NET CheckBoxList项目的索引及其文本值
How to get the index of selected ASP.NET CheckBoxList items and the text value of it using jQuery
我使用了列出的代码,但对我不起作用.
I used the code listed but it did not work with me.
前两行将返回带有"undefined"字符串的消息
First two lines will return message with "undefined" string
var index = $('#<%=chkListGroups.ClientID %>').attr("selectedIndex");
alert(index);
后两行将返回空消息.
var text = $('#<%=chkListGroups.ClientID %>').val();
alert(text);
var indices = '';
$('#<%=chkListGroups.ClientID %> input:checkbox').each(function (index) {
if ($(this).filter('input:checkbox:checked').length == 1) {
indices += (index + ' ');
}
});
alert(indices);
应打印索引.
如果用文本值"表示从ASP.NET中ListItem元素的Value属性返回的内容,则它不会直接自动包含在网页中.解决此问题的一种方法是在页面的.NET代码内创建Value属性的javascript数组,然后使用与上面类似的方法在数组中查找值.如果该数组称为"valueArray",则可以将上面"if"正文中的代码替换为
If by "text value" you mean what would be returned from the Value property of the ListItem element within ASP.NET, it is not directly included in the web page automatically. One way to address this issue would be to create a javascript array of the Value properties within the .NET code for the page, then use something similar to the above to look up the value in the array. If the array is called "valueArray", then you would could replace the code in the body of the "if" above with
indices += (valueArray[index] + ' ');
我希望这会有所帮助-我当然欢迎任何问题.
I hope this helps -- I certainly welcome any questions.
编辑(由提交者发表评论):
EDIT (following comment by submitter):
如果您希望每个已检查项目都带有可见标签,这应该可以:
If you want the visible label of each checked item, this should work:
var text = $('#<%=chkListGroups.ClientID %> input:checkbox:checked').siblings('label').text();
alert(text);