您好,如何使用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);
            });
        }发布于 2010-05-22 01:06:35
使用集合的选择器,然后对您感兴趣的元素使用index,这里是选中的元素。
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); 
});  发布于 2010-05-22 01:04:44
如果需要上一次选中复选框的索引,则如下所示
var query = (from o in CheckBoxList1.Items.OfType<ListItem>()  
                     where o.Selected  
                     select o).Take(1).Reverse().ToList();   发布于 2010-05-22 01:14:15
get value: $("input:checked").val();
get id: $("input:checked").attr('id');
//or this:
var counter=0;
$('input:checkbox').each(function(){
   if($(this).is(":checked"))
   {
     var Index = counter;
   }
   else{
     counter++;
   }
   //So Index is what you want.
});我还没有测试过它,但它类似于下面的代码。我希望它能帮助你
https://stackoverflow.com/questions/2884014
复制相似问题