2

こんにちは、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);


            });
        }
4

3 に答える 3

6

コレクションのセレクターを使用してから、関心のある要素(ここではチェックされている要素)でインデックスを使用します。

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-21T17:06:35.283 に答える
0

最後に選択したチェックボックスのインデックスが必要な場合は、

var query = (from o in CheckBoxList1.Items.OfType<ListItem>()  
                     where o.Selected  
                     select o).Take(1).Reverse().ToList();   
于 2010-05-21T17:04:43.963 に答える
0
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.
});

私はこれをテストしていませんが、このコードのようなものです。お役に立てば幸いです

于 2010-05-21T17:14:15.257 に答える