Jquery使用复选框选择表行

我有一张桌子

row 1 A
row 2 B
row 3 C

我希望在每行单元格中(当单击按钮时)检查其复选框的行

我试过filter

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

这很简单但我看不到我错过的东西……

这是jsfiddle链接……

你快到了 :)

如果您希望选中复选框的行中的所有

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

例如 :

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

更详细的例子是关于JsFiddle。

BTW
.filter(':has(:checkbox:checked)')可以写成.has(':checkbox:checked')如果你像我一样,更容易阅读。

你可以试试这段代码:

 $('#test').click(function(){ $('td.cb:checked').parents('tr').each( //get row values ); } 

HTH!

 // 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; } } });