您好, 如何使用jquery从checkboxlist控件中获取复选框的selectedindex?
更新:
此代码给我的选定索引等于 0,请告知
<asp:CheckBoxList ID="chkListGroups" runat="server"
style="position:absolute; top: 1115px; left: 745px; bottom: 371px;"
DataSourceID="SqlDSGroups" DataValueField="Groups"
onclick="test()">
</asp:CheckBoxList>
....................
java script function
.....................
function test(){
$('#<%=chkListGroups.ClientID %>').click(function() {
var selectedIndex = $('#<%=chkListGroups.ClientID %>').index($(this));
alert(selectedIndex);
});
}
最佳答案
对集合使用选择器,然后对您感兴趣的元素使用索引,这里是选中的元素。
var checkboxes = $('input:checkbox');
var selectedIndex = checkboxes.index(checkboxes.find(':checked'));
要获取单击的复选框的索引,请使用:
$('input:checkbox').click( function() {
var selectedIndex = $('input:checkbox').index( $(this) );
... now do something with it...
});
编辑:根据您的代码示例:
var checkboxes = $('#<%=chkListGroups.ClientID %>').find('input:checkbox');
checkboxes.click(function() {
var selectedIndex = checkboxes.index($(this));
alert(selectedIndex);
});
关于jquery - 如何使用jquery从checkboxlist控件中获取复选框的选定索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2884014/