JQuery sélection de lignes de tableaux avec case à cocher

j'ai une table

<table id="rowclick2">
  <tbody>
    <tr >
      <td class="cb"><input type="checkbox" value="yes"></td>
      <td>row 1</td>
       <td>A</td>
    </tr>    
    <tr >
      <td class="cb"><input type="checkbox" value="yes" ></td>
      <td>row 2</td>
       <td>B</td>
    </tr>    
    <tr>
      <td class="cb"><input type="checkbox" value="yes"></td>
      <td>row 3</td>
      <td>C</td>  
    </tr>    
  </tbody>
</table>

où je veux obtenir chaque cellule (quand le bouton est cliqué) dans une rangée dont les cases à cocher sont cochées

j'ai essayé le filtre

  $('#test').click(function(){

      $('#rowclick2 tr').filter(':has(:checkbox:checked)').each(
          //get row values
      );
    });

c'est assez simple mais je ne vois pas ce que je manque...

Ici est le lien jsfiddle...

21
demandé sur Brian Tompsett - 汤莱恩 2011-03-25 11:47:17

3 réponses

Vous y êtes presque :)

Si vous souhaitez que tous les <td> s dans les lignes où la case est cochée:

$('#rowclick2 tr').filter(':has(:checkbox:checked)').find('td');

E. G.:

$('#rowclick2 tr').filter(':has(:checkbox:checked)').find('td').each(function() {
    // this = td element
});

plus élaboré exemple est sur JsFiddle.

BTW

.filter(':has(:checkbox:checked)') peut être écrit comme .has(':checkbox:checked') si vous, comme moi, trouvent que plus facile à lire.

38
répondu jensgram 2011-03-25 08:59:01

Vous pouvez essayer ce code:

$('#test').click(function(){

    $('td.cb:checked').parents('tr').each(
          //get row values
      );
}

HTH!

2
répondu SubniC 2011-03-25 08:54:51
// select row and check box function condition
        $('#file_module_mainTableId').on('click', 'tbody tr', function(e){
            //e.preventDefault();
            var $this = $(this);
            // Detecting ctrl (windows) / meta (mac) key.
            if (e.ctrlKey || e.metaKey)
            {
                if($this.hasClass('ui-selected')){
                    $this.removeClass('ui-selected');
                    $this.find('input[type=checkbox]').removeAttr('checked');
                    if (e.target.type !== 'checkbox') {
                        $(':checkbox', this).trigger('click');
                    }
                }else{
                    $this.addClass('ui-selected')
                    $this.find('input[type=checkbox]').attr('checked', 'checked');
                }
            }
            // Detecting shift key
            else if (e.shiftKey)
            {
                // Get the first possible element that is selected.
                var currentSelectedIndex = $('#file_module_mainTableId tbody tr.ui-selected').eq(0).index();
                // Get the shift+click element
                var selectedElementIndex = $('#file_module_mainTableId tbody tr').index($this);
                // Mark selected between them
                if (currentSelectedIndex < selectedElementIndex)
                {
                    for (var indexOfRows = currentSelectedIndex; indexOfRows <= selectedElementIndex; indexOfRows++)
                    {
                         $('#file_module_mainTableId tbody tr').eq(indexOfRows).addClass('ui-selected');
                         $('#file_module_mainTableId tbody tr').eq(indexOfRows).find('input[type=checkbox]').attr('checked', 'checked');
                    }
                }
                else
                {
                    for (var indexOfRows = selectedElementIndex; indexOfRows <= currentSelectedIndex; indexOfRows++)
                    {
                         $('#file_module_mainTableId tbody tr').eq(indexOfRows).addClass('ui-selected');
                         $('#file_module_mainTableId tbody tr').eq(indexOfRows).find('input[type=checkbox]').attr('checked', 'checked');
                    }
                }    
            }
            else
            {
                if (e.target.type !== 'checkbox') {
                    $(':checkbox', this).trigger('click');
                }
            }
        });
        // checkbox class section  
        $(".chkbox").change(function(e){
            if ($(this).is(":checked")){
                $(this).closest('tr').addClass("ui-selected");
            } else {
                $(this).closest('tr').removeClass("ui-selected");
            }
        });
        // clrt + A select all table row 
        $(document).on('keydown', function(e){
            if(!$('body').hasClass('.modal-open')){ // use this condition for modal - deprecated here
                if(e.metaKey || e.ctrlKey && e.keyCode == 65){
                    $('#file_module_mainTableId tbody tr').addClass('ui-selected');
                    $('#file_module_mainTableId tbody tr').find('input[type=checkbox]').attr('checked', 'checked');
                    e.preventDefault();
                    return false;
                }
            }
        });
0
répondu Arpit Pithadia 2015-07-15 10:08:34