0

如何获得复选框相对于其组的位置?

我尝试创建 for 循环,但无法使其正常工作。经过一天的研究,我想通过添加,

  var indexValue = ($(this).attr('tabindex')); 

我可以得到复选框的位置,然后使用该值来选择正确的项目。你能告诉我我在这里做错了什么吗?

     <script type="text/javascript">

         var $checkboxes = $('input[type="checkbox"]');

         $(document).ready(function () {
             var nl = 4;
             var nos = $('#listTable #searchString').length;
             // alert(nos);
             $.each($checkboxes, function () {
                 if ($(this).is(':checked')) {
                     //  alert('checked');
                     var indexValue = ($(this).attr('tabindex'));

                     $('#listTable .daylists').eq(indexValue).css('border', '2px dashed blue').val($('option:first', this).val());
                     $('#listTable .teacherlists').eq(indexValue).css('border', '2px dashed blue').val($('option:first', this).val());

                 }


             });

         });


    </script>
4

3 回答 3

0
$.each($checkboxes, function(index, element) {
    // index is the position you are at within this loop, starts at 0
});

http://api.jquery.com/jQuery.each/

于 2013-04-15T15:04:43.413 回答
0

没有看过您的 HTML,您可能想使用index

var i = $checkboxes.index();

或依赖传递给的索引参数each

$.each($checkboxes, function (i) {
     // i is your index
});
于 2013-04-15T15:06:42.670 回答
0

如果复选框都是父元素下的兄弟,您可以使用:

$(this).index()
于 2013-04-15T15:19:09.717 回答